Before we get into using the convenience methods that come with views, let`s talk about the request and response loop. So when there is a request to the Django server, several things happen. One of them is Middleware.
Middleware — it is a cross between a request and a response. It`s like a window through which data flows. As in the window, light comes in and out of the house. Similarly, when a request is made, the request, when made, travels through the middleware to the views, and the data is passed through the middleware as a response.
Here is the default middleware installed in Django.
You can add your middleware. We`ll discuss this in future articles.
Django uses request and response objects to communicate state through the system.
When the page is requested, Django creates an HttpRequest object that contains the metadata about the request. Django then loads the appropriate view by passing the HttpRequest as the first argument to the view function. Each view is responsible for returning an HttpResponse object.
You can use the following attributes with HttpRequest for advanced manipulation
|HttpRequest.scheme||A string representing the scheme of the request (HTTP or HTTPs usually ).|
|HttpRequest.body||It returns the raw HTTP request body as a byte string.|
|HttpRequest.path||It returns the full path to the requested page does not include the scheme or domain.|
|HttpRequest.path_info||It shows path info portion of the path.|
|HttpRequest.method||It shows the HTTP method used in the request.|
|HttpRequest.encoding||It shows the current encoding used to decode form submission data.|
|HttpRequest.content_type td>||It shows the MIME type of the request, parsed from the CONTENT_TYPE header.|
|HttpRequest.content_params||It returns a dictionary of key / value parameters included in the CONTENT_TYPE header.|
|HttpRequest.GET||It returns a dictionary-like object containing all given HTTP GET parameters.|
|HttpRequest.POST||It is a dictionary-like object containing all given HTTP POST parameters.|
|HttpRequest.COOKIES||It returns all cookies available.|
|HttpRequest.FILES||It contains all uploaded files.|
|HttpRequest.META||It shows all available Http headers.|
|HttpRequest.resolver_match td>||It contains an instance of ResolverMatch representing the resolved URL.|
You can use the following methods with HttpRequest for advanced manipulation
|HttpRequest.get_host ()||It returns the original host of the request. td>|
|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. td>|
You can use the following attributes with HttpResponse for advanced manipulation
|HttpResponse.content||A bytestring representing the content, encoded from a string if necessary.|
|HttpResponse.charset||It is a string denoting the charset in which the response will be encoded.|
|HttpResponse.status_code||It is an HTTP status code for the response.|
|HttpResponse.reason_phrase td >||The HTTP reason phrase for the response.|
|HttpResponse.streaming||It is false by default.|
|HttpResponse.closed||It is True if the response has been closed.|
You can use the 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) em>||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) em> td > || 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.tr>|
|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.tr>|
This book project was first presented to me during my first week in my current role of managing the data mining development at SAS. Writ- ing a book has always been a bucket‐list item, and I was ver...
Shabbir Challawala has over 8 years of rich experience in providing solutions based on MySQL and PHP technologies. He is currently working with KNOWARTH Technologies. He has worked in various PHP-base...
While there is no arguing about the staying power of the cloud model and the benefits it can bring to any organization or government, mainstream adoption depends on several key variables falling into ...
If you can program, you are ready to grapple with Bayesian statistics. In this book, you'll learn how to solve statistical problems using Python code instead of math formulas, using discrete probabili...