Django Request-Response Cycle – An easy to follow guide

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

We offer you a brighter future with industry-ready online courses - Start Now!!

All backend technologies and web APIs are based on one system that is The Request-Response Cycle. The system is responsible for data interchange between client and servers. Even the new technologies based on micro-services use the Request/Response cycle.

Request/Response objects are transmitted over the web. Content like images, HTML, CSS, JavaScript is all Response Objects. In this article, we will learn about these objects.

We will learn how Django interacts with the web which means the whole process of Django getting a request and giving a response.

Ready to learn? Let’s get started.

How does Web Work?

It’s simple yet a very important aspect to understand. So, basically, any application over the internet works by passing information. This information is often passed as objects. The objects mentioned here comprises of various files or properties. These objects can contain files as attachments.

It is a very similar concept to emails.


These interchanges between various servers and client machines following a set of rules. That set of rules is called HTTP. HTTP stands for HyperText Transfer Protocol. It’s simply a set of rules, a common standard followed by devices to connect to each other over the internet.

The information sent or received is given the name of request and response.
Request Objects contain the request made by the client. The server responds with the appropriate information according to Request Object.

On the other hand, the Response Object is information served by the server to the client. It all makes sense now? Wait, there’s still more:

The below figure clearly explains how the web works. Well, it does that on a very high-level perspective. In this figure, both the client and server are using HTTP. If both of them don’t have the same protocol, the connection can not be done.

Just like humans need to have a common language to understand each other. The same way, various computers and machines over the internet need to have the same protocol. At least the one through which they communicate.

django request response cycle


Now, it’s time to understand Request. The requests are smaller in size than response. Basically, a Request is a “request made by the client” to the server. Any URL that gets searched is a request.

But, that’s not the only type of Request. Request objects also give information to the server. The forms we fill, the images we upload to the server are also Requests. When we upload this information, it is transferred as attributes of the Request object.

The server can then access that information and call its own functions.

As you can see in the image, we have a request for http://localhost:8000. From that, we also get a response.

GET Request - Django Request Response Cycle


Responses are generally heavier in bandwidth than Request. The Response objects usually contain HTML, CSS, JavaScript, image, video files, etc. There are other files as well, which can be served by the server as a response. Response objects also have various parts.

Response Object - Django Request Response Cycle

This is the response object we actually get from the server. It has various parts to it. The response object we are discussing has 5 headers. Any response or request objects have a part to them called HTTP headers. These HTTP headers contain various information regarding the response.

You can consider it as metadata of response objects. It tells the browser that from where the response came and what type of data it contains. There are many properties in the HTTP header.

So, in the above example response, we can see the Content-Type attribute. That attribute’s values are text/html; charset=utf-8. This means that the response contains an HTML file.

Django Request-Response Cycle

Django’s handling of Http Requests and Response is unique in its own ways.

Django can be considered as an application on the server. Its main task is to process the request received by the server. Then it calls functions and provides a response. This is a brief of how Django interprets Request and provides Response.

Django Middlewares - Django Request Response Cycle

Whenever a request comes into Django, it is handled by middlewares. When the Django server starts, the first thing it loads after is middlewares.

The Request is processed by various middlewares one at a time. So, from the above list, when the request comes it will be passed through the security middleware. If it deems it to be unhealthy, then it will not let the request go any further.

Otherwise, the authentication requests are handled by authentication middleware. That comes after 4 middleware classes that didn’t find how to handle that request.

Once, our request is processed by these middlewares it is passed to the URL Router. The URL router simply extracts the URL from the request and will try to match it to defined urls. - Django request response cycle

Once, we get a matching URL, the corresponding view function is called. The view function will get various attributes and other URL parameters. Also, the view function will be able to access files from the requests. These Requests are considered to be HttpRequest class objects.

The requests module is a Python module that provides various methods for Request objects.

Once, the view function has been executed, it’s time to give a response. The response is given in the form of HttpResponse. The response is not limited to that. The Response can be PDF, JSON, CSV. That is one of the features of Django. It provides built-in support to provide responses of various types.

When the response is a render, it will look for the HTML. The HTML page to be the server is processed by Django Templating Engine. Once that completes, the Django simply sends the files as any server would. That response will contain the HTML and other static files.

Let’s take an example in which we will access the request data and print that in the server command line. Ultimately it’s just text.

Here is the view function.

view function - Django request & response

When I run the server and request something in the browser, it will print this information on the command line.

Here, you can see the command line output.

command line output - Django request response cycle

As you can see, there are lots of information for a request like this.


And since it’s also serving an HTML page, you can try these with any of your view functions.


I hope this article on the request and response cycle provided to be useful in your understanding of the Django framework. We understood how Django handles any request coming its way. The request handling of any framework is an important concept.

Since we have been implementing the concepts in various projects, we need not develop a new one here. I hope you like this post.

Clear with the Django request and response cycle? Mention your queries & feedback in the comment section.

Keep learning!

Your opinion matters
Please write your valuable feedback about DataFlair on Google


7 Responses

  1. Kariuki says:

    Thank you for these super amazing tutorials, God bless you!

  2. Hussam says:

    You completely forgot about the middleware being processed for responses as well

  3. seven says:

    and it’s very detailed!

  4. amit says:

    short and clear explanation ..thanks

  5. Zubair says:

    1. not explained, that how the request reaches the middleware
    2. ambiguous information that if the HTML response is rendered before returning to middleware or after that
    The sequence is also ambiguous.

Leave a Reply

Your email address will not be published. Required fields are marked *