Home >>Django Tutorial >Django Request and Response

Django Request and Response

Django Request and Response

The client-server architecture includes two major components request and the response. The Django framework uses the client-server architecture to implement the web applications.

When a client requests for any resource then a HttpRequest object is created and the correspond view function is called that returns the HttpResponse object.

To handle this request and response, Django provides the HttpRequest and HttpResponse classes. Each class has it’s own attributes and methods.

Django HttpRequest

This class is defined in the django.http module and used to handle the client side requests.

Django HttpRequest Attributes

Attribute Description
HttpRequest.scheme This is a string representing the scheme of the request.
HttpRequest.body This returns the raw HTTP request body as a byte string.
HttpRequest.path This returns the full path to the requested page.
HttpRequest.path_info This shows path info portion of the path.
HttpRequest.method This shows the HTTP method used in the request.
HttpRequest.encoding This shows the current encoding used to decode form submission data.
HttpRequest.content_type This shows the MIME type of the request, parsed from the CONTENT_TYPE header.
HttpRequest.content_params This returns a dictionary of key/value parameters included in the CONTENT_ TYPE header.
HttpRequest.GET This returns a dictionary-like object containing all given HTTP GET parameters.
HttpRequest.POST This is a dictionary-like object containing all given HTTP POST parameters.
HttpRequest.COOKIES This returns all the cookies available.
HttpRequest.FILES This contains all the uploaded files.
HttpRequest.META This shows all the available Http headers.
HttpRequest.resolver_match This contains an instance of ResolverMatch representing the resolved URL.

Django HttpRequest Methods

Attribute Description
HttpRequest.get_host() This returns the original host of the request.
HttpRequest.get_port() This returns the originating port of the request.
HttpRequest.get_full_path() This returns the path, plus an appended query string, if applicable.
HttpRequest.build_absolute_uri (location) This returns the absolute URI form of location.
HttpRequest.get_signed_cookie This 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() This returns True if the request is secure.
HttpRequest.is_ajax() This returns True if the request was made via an XMLHttpRequest.

Django HttpRequest Example

// views.py

def methodinfo(request):  
    return HttpResponse("Http request is: "+request.method)  

// urls.py


Now, Start the server and get access to the browser. It will show the request method name at the browser.

Django HttpResponse

This class is a part of the django.http module. It is responsible for generating the response corresponds to the request and back to the client.

This class contains various attributes and methods that are given below.

Django HttpRequest Methods

Attribute Description
HttpResponse.content This is a bytestring representing the content, encoded from a string if necessary.
HttpResponse.charset This is a string denoting the charset in which the response will be encoded.
HttpResponse.status_code This is an HTTP status code for the response.
HttpResponse.reason_phrase This is the HTTP reason phrase for the response.
HttpResponse.streaming This is false by default.
HttpResponse.closed This is True if the response has been closed.
Method Description
HttpResponse.__init__ This is used to instantiate an HttpResponse object with the given page content and content type.
HttpResponse.__setitem This is used to set the given header name to the given value.
HttpResponse.__delitem This deletes the header with the given name.
HttpResponse.__getitem This returns the value for the given header name.
HttpResponse.has_header This returns either True or False based on a case-insensitive check for a header with the provided name.
HttpResponse.setdefault This is used to set default header.
HttpResponse.write This is used to create response object of file-like object.
HttpResponse.flush() This is used to flush the response object.
HttpResponse.tell() This method makes the HttpResponse instance a file-like object.
HttpResponse.getvalue() This is used to get the value of HttpResponse.content.
HttpResponse.readable() This method is used to create the stream-like object of HttpResponse class
HttpResponse.seekable() This is used to make response object seekable.

Django HttpResponse Methods

We can use these methods and attributes to handle the response in the Django application.

No Sidebar ads