Skip to content
Related Articles

Related Articles

Django Request and Response cycle – HttpRequest and HttpResponse Objects
  • Difficulty Level : Basic
  • Last Updated : 26 Sep, 2019

Prerequisite – Views In Django | Python

Before we jump into using convenience methods that views come with, Let’s talk about Request and Response cycle. So when a Request happens to a Django server, a couple of things go on. One of them is Middleware.

Middleware is like a middle ground between a request and response. It is like a window through which data passes. As in a window, light passes in and out of the house. Similarly when a request is made a request when made moves through middlewares to views and data is passed through middleware as a response.
Here are the default middlewares installed in Django.


You can add your middlewares. We will be discussing that in the next articles.

Request and Response Objects –

Django uses request and response objects to pass state through the system.

When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function. Each view is responsible for returning an HttpResponse object.

HttpRequest and HttpResponse object Example

  • To explain these objects let’s create a view home as below in

    # importing HttResponse from library
    from django.http import HttpResponse
    def home(request):
        # request is handled using HttpResponse object
        return HttpResponse("Any kind of HTML Here")
  • To handle the request let us map a URL to this view in

    # importing view from
    from .views import home
    urlpatterns = [
        path('', home),
  • Now you can run the sever to see the following in browser

HttpRequest Attributes – Django

You can use following attributes with HttpRequest for advanced manipulation

HttpRequest.schemeA string representing the scheme of the request (HTTP or HTTPs usually).
HttpRequest.bodyIt returns the raw HTTP request body as a byte string.
HttpRequest.pathIt returns the full path to the requested page does not include the scheme or domain.
HttpRequest.path_infoIt shows path info portion of the path.
HttpRequest.methodIt shows the HTTP method used in the request.
HttpRequest.encodingIt shows the current encoding used to decode form submission data.
HttpRequest.content_typeIt shows the MIME type of the request, parsed from the CONTENT_TYPE header.
HttpRequest.content_paramsIt returns a dictionary of key/value parameters included in the CONTENT_TYPE header.
HttpRequest.GETIt returns a dictionary-like object containing all given HTTP GET parameters.
HttpRequest.POSTIt is a dictionary-like object containing all given HTTP POST parameters.
HttpRequest.COOKIESIt returns all cookies available.
HttpRequest.FILESIt contains all uploaded files.
HttpRequest.METAIt shows all available Http headers.
HttpRequest.resolver_matchIt contains an instance of ResolverMatch representing the resolved URL.

HttpRequest Methods – Django

You can use following methods with HttpRequest for advanced manipulation

HttpRequest.get_host()It returns the original host of the request.
HttpRequest.get_port()It returns the originating port of the request.
HttpRequest.get_full_path()It returns the path, plus an appended query string, if applicable.
HttpRequest.build_absolute_uri (location)It returns the absolute URI form of location.
HttpRequest.get_signed_cookie (key, default=RAISE_ERROR, salt=”, max_age=None)It returns a cookie value for a signed cookie, or raises a django.core.signing.BadSignature exception if the signature is no longer valid.
HttpRequest.is_secure()It returns True if the request is secure; that is, if it was made with HTTPS.
HttpRequest.is_ajax()It returns True if the request was made via an XMLHttpRequest.

HttpResponse Attributes – Django

You can use following attributes with HttpResponse for advanced manipulation

HttpResponse.contentA bytestring representing the content, encoded from a string if necessary.
HttpResponse.charsetIt is a string denoting the charset in which the response will be encoded.
HttpResponse.status_codeIt is an HTTP status code for the response.
HttpResponse.reason_phraseThe HTTP reason phrase for the response.
HttpResponse.streamingIt is false by default.
HttpResponse.closedIt is True if the response has been closed.

HttpResponse Methods – Django

You can use following methods with HttpResponse for advanced manipulation

HttpResponse.__init__(content=”, content_type=None, status=200, reason=None, charset=None)It is used to instantiate an HttpResponse object with the given page content and content type.
HttpResponse.__setitem__(header, value)It is used to set the given header name to the given value.
HttpResponse.__delitem__(header)It deletes the header with the given name.
HttpResponse.__getitem__(header)It returns the value for the given header name.
HttpResponse.has_header(header)It returns either True or False based on a case-insensitive check for a header with the provided name.
HttpResponse.setdefault(header, value)It is used to set default header.
HttpResponse.write(content)It is used to create response object of file-like object.
HttpResponse.flush()It is used to flush the response object.
HttpResponse.tell()This method makes an HttpResponse instance a file-like object.
HttpResponse.getvalue()It is used to get the value of HttpResponse.content.
HttpResponse.readable()This method is used to create stream-like object of HttpResponse class.
HttpResponse.seekable()It is used to make response object seekable.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :