Building Web Applications with Django ,A Practical Guide.🏗️ 🚀🐍🌐
Capsule 1, Introduction to Django
Greetings comrades, it’s another brand-new day and we need to take our KodeCapsule of the week. I will be starting a tutorial blog series on one of the most popular python web development framework (WDF), Django. This tutorial series will take you from an absolute beginner to an expert in using Django to build web apps and REST APIs. So, stay turned and enjoy the cruise as we dive into the world of Django.
Outline of Article
Introduction
What is Django?
Benefits of Django
Architecture of Django
Installing and Setting up Django
Django project folder structure
Conclusion
References
Introduction
What is a Web development Framework (WDF)?
Before we take a look at what Django is, lets first get to understand what a WDF and why we need web frameworks. In Simple terms a web development Framework (WDF) is a collection of tools that are purposely designed to help in the development of web applications including web services, web resources and web APIs. WDFs provide a standard approach to build and deploy web applications on the World Wide Web. The goal of WDFs is to automate the overhead associated with common activities that are carried out in web development. Some popular WDFs are,_ .NET (C#), Django(Python), Ruby on Rails (Ruby) Express (JavaScript/Node.js), Laravel (PHP), Spring Boot(Java)._
What is Django?
Django, as describe on the Django website, ( The web framework for perfectionists with deadlines) is a high-level Python web framework that encourages rapid development, clean and pragmatic design. It was created to help web developers move from applications concept to completion as quickly as possible. With Django, you can build and deploy web applications in less time with few lines of code. Django is designed by skilled developers and helps to handle a lot of the hassle of web development, so you can focus on writing your application without having to reinvent the wheel. Django is also an open-source project that was released in 2005. You can learn more about Django here.
Benefits of Django
Fast Development: Django is designed based on the “batteries-included” philosophy which simply means that Django comes with a lot of baked in features and tools out of the box for faster application development. Django offers a lot of built-in features (Django’s ORM,Authentication system etc) and functionalities that developers commonly use when building applications.
Highly Secured: Django has a lot of build in security features that helps app developers avoid a number of common security mistakes by providing a secure way to manage user accounts and passwords through its built-in authentication system. Django also has features that offer protection against common web exploits such as cross-site scripting (XSS), cross-site request forgery (CSRF) and SQL injection.
Scalable: One other benefit of using Django is that it enables your applications to scale smoothly. Django can scale quickly to handle huge volumes of traffic demands from the smallest to the largest web applications.
Versatile: Whether you’re building a content management system, a social network, a REST API, machine learning application, Scientific computing platforms, Django provides you with all the tools you need to build any type of web application.
Architecture of Django
Django is designed based on the Model View Template (MVT) architecture. MVT is a software design pattern.
Model: The model is the data access layer that is responsible for handling the data-logic and defines the structure for storing the application data in a database. Models are independent of the types of database used because Django has an inbuilt Object relational Mapper (ORM) that maps Python classes to database tables.
View: The view is the “glue” between the Model and the Template. The view handles the business logic. The view receives request from the user via a template, process the data using a model and return a response. A View in Django is the same as a controller in other frameworks like spring boot that uses the MVC architecture.
Template: The presentation layer is the template which is responsible for handling the layout and structure of the user facing application. The template defines how data should be presented to the user. The template contains static content such as HTML, CSS and JavaScript usually injected with Django Template Language (DTL) which allows dynamic content to be inserted into a template.
Note: Django has a URL dispatcher that maps URLS to corresponding views. Find more details about the URL dispatcher here.
Installing and Setting up Django
Step 1. Installing Python on your Computer
You have to have some basic knowledge of Python programming language.
Make sure you have Python installed on your computer. If you have not installed python visit the python website and follow the instructions for installing python on your preferred operating system. I will be using Windows OS in this tutorial series. Download and install Python.
Step 2. Setting Up a Virtual Environment, Installing Django and starting your first project.
A virtual environment is an isolated environment in which you can work on your Python projects independently of the Python that is installed on your system. You can set up the libraries and dependencies that your project needs without affecting the main Python libraries installed on your system.
1.Install virtualenv: virtualenv is a tool that is use to create isolated Python environments. To install virtualenv run this command in your terminal.
pip install virtualenv
2.Create a Virtual environment: Create a folder for your project and cd into the project folder in your terminal and run this command.
py -m venv django_venv
3.Activate virtual environment : To activate the virtual environment cd into your virtual environment using this command.
django_venv/scripts/activate
4.Install Django: Install Django in the virtual environment using this command.
pip install django
5.Confirm Django installation: To confirm that Django is install type this command. This command list Django and other modules that Django uses.
pip freeze
6.Start your first project: To start your first project run this command
django-admin startproject myproject .
7.Run development Server: cd into myproject folder and start the development server by running this command
python manage.py runserver
8.The development server will start at http://127.0.0.1:8000/ Copy the url to your browser. Congrats you have successfully installed and start a new project
Django project folder structure
Django organizes and structure your project into separate and different components. This structure encourages code reusability, maintainability, and scalability. When you created the new django project using this command, django-admin startproject myproject, in the previous section, the following directory structure was generated.
├── myproject/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
manage.py: This script is a command-line tool that allows you to interact with your Django project. It contains collection of commands that you can use to perform various task in your Django project like starting the development server, running tests, and perform other administrative tasks.
init.py: This is an empty file that informs python to treat that directory as a python package.
settings.py: This script is the heart of your Django project. It contains all the settings and configurations for your project and determines how Django behaves. Some common settings include database settings, installed apps, template directories, middleware and more. Settings Reference: https://docs.djangoproject.com/en/5.0/ref/settings/
urls.py: This script defines all the URL patterns for your project. It acts as a URL dispatcher, mapping all incoming URLs (request) to specific views that handle those requests. Refer to the official Django documentation for a deeper understanding of URL patterns and the urls.py file, https://docs.djangoproject.com/en/5.0/topics/http/urls/
wsgi.py: This script is used by the web server to serve your application. Web Server Gateway Interface (WSGI) acts as a mediator that is responsible for conveying communications between a web server and a Python web application. It’s essential for deploying Django apps.
asgi.py:This script acts as the entry point for ASGI compatible web servers to serve your Django project. Asynchronous Server Gateway Interface (ASGI) is used to handle asynchronous tasks. ASGI is a spiritual successor to WSGI, intended to provide a standard interface between async-capable Python web servers, frameworks, and applications.
Conclusion
Well well , we have come to the end of the this weeks Kodecapsule. In this article we were introduced to Python web framework, Django, the benefits of using Django, it’s architecture and created our first project. In the next article we will take a look at Django apps, creating our own apps and getting our hands “dirty”.
Be sure to watch out for the articles in this series as I take you from Zero to an expert in Django.
_Your Suggestions and Comments are always welcome.
Kuseh Wewoliamo_
References
https://www.djangoproject.com/
https://docs.djangoproject.com/en/5.0/ref/settings/
https://en.wikipedia.org/wiki/Django_(web_framework)
https://medium.com/@commbigo/wsgi-vs-asgi-for-python-web-development-9d9a3c426aa9
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
https://builtin.com/data-science/wsgi
https://www.freecodecamp.org/news/how-to-setup-virtual-environments-in-python/
Top comments (0)