Life Cycle of Django

Life Cycle of Django

Request - Response life cycle in Django

 Thebasic principle of http protocol is the client sends a request to the server based on the request data, and the server sends a response to the client. We need a webserver and WSGI server while setting up the Django application .Webserver helps us serving static files &content. If it is not served by the web server static files, then it has to served by the WSGI server, which results in more num of request to the server. So gradually slow down the application performance web server balance request load on the server. So highly recommended using the webserver. 

A client can be known as a piece of software that can send a request by following the HTTPS/HTTP. In general terms, it is considered a web browser. We are using one of the variations of "Nginx, uWSGI and Django" or "Nginx, gunicorn and Django" or "Apache, mod wsgi and Nginx" when deploying the Django framework on the database.

When a request is sent by a client to the 0server that is forwarded to the webserver for the first time, it includes the rules of configuration to forward the request to the WSGI server or to handle it on its own.

Layers in Django Application

There are six layers in Django application

1-Request Middlewares

2-URL Dispatcher(URL Router)

3-Context Processors

4-Views

5-Template Renderers

6-Response Middlewares

The Application middlewares must manage the application when it comes in. Several middlewares we can have. This can be found in the project (settings.py). As handling the application, Django requests middlewares follows the command. Suppose we have middleware requests in order A, B, C, then the request is handled first by middleware A and then B, and then C.Django has a range of generic middlewares. We can also write middlewares of our own or tradition. It will be sent to the URL Router or URL dispatcher after the request handled by the middlewares.

URL Router takes the request from the middleware client, and from the request, it takes the URL Route. The URL router will attempt to match the request path to the available URL patterns based on the URL pathThese patterns of URLs are regular expressions. The request will be submitted to the View associated with the URL after matching the URL path with available URL patterns.

We are now in the Views business logic layer.Views use request and request data (data sent in GET, POST, etc.) to process the business logic.Using the request context processors, after processing the request in the view, it adds the context information that will help Template Renderers make the template to generate the HTTP response

Again, the application must return to the middleware of the Response for processing. Answer middlewareprocess the request, and before sending it back to the client(Browser), add or change the header/body information. After loading and showing the app to the end-user.

Application Life Cycle

A plan is a total of a large number of applications. That application has a purpose and can be reused for another task, such as the contact form on a website can be an application and can be reused for others. 

Create an Application

We assume we are in the project folder. In our main “myproject1” folder, the same folder then manage.py –

$ python manage.py startapp myapp

We just created myappl application, and like a project, Django creates a “myappl” folder with the application structure

myappl/

   __init__.py
    admin.py
    models.py 
    tests.py
    views.py 

Life Cycle of Django