1.

How a request is processed in Django?

Answer»

We know that all web application uses HTTP/HTTPS protocol. The basic principle of the HTTP protocol is Client sends a request to the server and the data server sends a response back to the client based on request data. We need a web server and WSGIserver while setting up a Django application on the server. Without a web server, WSGI server will result in a more number of requests resulting in gradually slow down the application performance. The web server will help in balancing the requests load on the server. 

The client is a piece of software which sends a request by following HTTP/HTTPS protocol and mostly we consider Web Browser as a client. "Nginx, uWSGI and Django" or "Nginx, gunicorn and Django" or "Apache, mod_wsgi and Nginx" are the THREE types of combinations in Django application deployment on the server and we can use any one of the combinations. Now let us discuss on how a request is processed and response is created and sent to the client.

When a client sends request it first passed to a web server. The request contains configuration rules to be dispatch to the WSGI server. WSGI server sends the request to the Django application.

For dealing with request-response lifecycle, Django application has the following layers -

  • Request middlewares
  • URL Router
  • Views
  • Context processors
  • Template Renderers
  • Response MIDDLEWARE

Any request that comes in is handled by Request middleware. There can be multiple middlewares and can FIND it in settings.py (project settings). While processing the request Django Request middlewares follow the order. Django has some default middlewares and we can also WRITE or customize middleware. Middleware process the request and submits it to the URL Router or URL dispatcher.

URL Router receives a request from the middleware and from the request it collects the URL path. From the URL path, the URL router tries to match the request path with available URL patterns. These patterns are in the regular EXPRESSION form. Once the URL path is matched with URL patterns the request is submitted to the View associated with URL.

Views are the business logic layer, which processes the business logic using request and request data. A request is processed in the view, it is sent to Context processor. Context processor adds context data that helps Template Renders to deliver the template to generate HTTP response and again this request will be sent back to Response middleware to process. Request middleware adds or modifies header information or body information before sending it to the client again.



Discussion

No Comment Found