Django
Web Framework
The Django framework adopts an MVT approach. The model is the data layer of the application. The view layer is responsible for processing the request and returning the response. The template is the presentation layer.
URL dispatcher is another crucial component of the Django architecture. The URL dispatcher is equivalent to Controller in the MVC architecture. The urls.py
module in the Django project's package folder acts as the dispatcher.
It defines the URL patterns. Each URL pattern is mapped with a view function to be invoked when the client's request URL matches it.
The URL patterns defined in each app under the project are also included in it.
When the server receives a request in the form of a client URL, the dispatcher matches its pattern with the patterns available in the urls.py and routes the flow of the application toward its associated view.
Virtualenv will isolate your Python/Django setup on a per-project basis. This means that any changes you make to one website won't affect any others you're also developing.
Model-View-Template
MVT is like MVC, except the re is no Controller and the Django Server itself performs the controller functions.
In Django, View is a python function that takes a web request(HTTP GET/POST/DELETE/UPDATE) and applies the neccessary logic to generate a web response(STRING, JSON/XML, HTML,ERROR STATUS).
Django uses a template containing static HTML elements and special Python code to generate dynamic web pages.
Project Structure
manage.py
is a command line interface used to interact with the django projectsettings.py
contains the settings and configurations for your django project.urls.py
contains the URL and routing definitions of your Django app.
View
Function-based View
Pros | Cons |
---|---|
Simple and easy to understand | Difficult to extend or reuse |
Explicit Code | Handles requests using conditional statements |
Mainly built for a specific functionality | may increade code complexity |
Class-based View
Pros | Cons |
---|---|
Reusable and Extendible | Code is harder to read |
Handles requests using class methods | Implicit code is hidden from developers |
Leverage built-in generic class-based views |
Generic class-based View
ListView
: represents a list of objectsDetailView
: represents the details of an objectFormView
: represents a form for submitting dataGroup of Date views: handles date-based data
Deployment
WSGI | ASGI |
---|---|
Web Server Gateway Interface | Asynchronous Server Gateway Interface |
Web Servers (Gunicorn, uWSGI, Apache, mod_wsgi) | Web Servers (Daphne, Hypercorn, Uvicorn) |
Last updated