Django Request-Response Cycle
All of the backend technologies and web APIs are based on one system that is Request and Response Cycle. The system is mainly responsible for interchanging the data between clients and servers.
So, are you ready to learn? Let’s get started.
How does the web work?
The web is everywhere and it's simple yet a very important aspect to understand. Basically any application over the internet works by passing information to the clients. This information is often passed as objects which comprises various files or properties.
It stands for Hypertext Transfer Protocol. It is a protocol used to transfer data over the web. It is the basic foundation of any data exchange over the Web. It is also known to be a client-server protocol, which means requests are generally initiated by the recipient, usually the Web browser. In response to the request a complete document is reconstructed from different sub-documents fetched, for instance text, layout description, images, videos and many more.
In the above shown figure, both the client and server are using HTTP. If both of them don’t have the same protocol, the connection can not be established. As we humans understand a common language to understand each other, in the same way various computers and machines over the internet need to have the same protocol.
What are Request and Response?
Requests consists of the following elements:
An HTTP method, usually has GET, POST like OPTIONS or HEAD that defines the operation the client wants to perform. Typically, a client wants to retrieve an information (using GET) or post the value of an HTML form (using POST).
HTTP/1.1 - This shows the version of the HTTP request
For example -
Responses consist of the following elements:
A status, indicating if the HTTP request was successful, or not, and why.
Optional headers such as content-type, server etc are used to convey additional information for the servers.
Now as we have covered the basic HTTP Request and Response. Let’s move a bit further,
Django handles the Http Requests and Response in its own unique ways -
Django Middlewares - Django Request Response Cycle
Whenever a request is made in Django, it is handled by middlewares. When Django server starts, the first thing it loads after settings.py is middlewares.
The Request will be processed by different middlewares one at a time. As shown in the above image, when the request comes it has to pass through the security middleware and If it deems it to be unhealthy, then it will not let the request go any further.
Once the request is processed by these middlewares it will be passed to the URL Router. The URL router simply grabs the URL from the request and it will try to match it with the defined urls in the urls.py.
Once the URL is matched, the corresponding view function will be called. The view function will get various attributes and other URL parameters and will also be able to access files from the requests. These Requests are considered to be HttpRequest class objects.
After execution of the view function, the response will be generated. The Response can be JSON or CSV, HttpResponse which is one of the features of Django. It gives a built-in support in order to provide responses of various types. When the response is rendered, it will look for the HTML. The HTML page on the server is processed by Django Templating Engine. Once that completes, Django simply sends the files as any server would which will contain the HTML and other static files.
So, here we have completed the basics of accessing the request and response in django.
As we have reached the end of the blog, I hope this will provide a clear understanding of how Request and Response work in the django framework. It’s a very important concept to know about the request handling of any framework.
Have any doubt or query regarding this article? Please comment down below for any query or feedback.
Leave a Comments