[Django] Middleware

Source: Internet
Author: User

Middleware intercepts all requests and directly returns response after processing in our own way. Therefore, it is necessary to understand the composition of middleware.

 

Initializer: _ init _ (Self)

For performance considerations, each enabled middleware is initialized only once in each server process. That is to say, _ init _ () is called only when the service process is started, but not when processing a single request.

For a middleware, the common reason for defining the _ init _ () method is to check its own necessity. If _ init _ () throws an exception Django. Core. Exceptions. middlewarenotused, Django will remove the middleware from the middleware stack.

When defining the _ init _ () method in middleware, no other parameters except the standard self parameter should be defined.

 

Request preprocessing function: process_request (self, request)

The call time of this method is After Django receives the request, but the URL is not resolved to determine the view to be run. Django transmits the corresponding httprequest object to it for modification in the method.

Process_request () should return the none or httpresponse object.

If none is returned, Django will continue to process this request, execute the subsequent middleware, and then call the corresponding view.

If the httpresponse object is returned, Django will no longer execute any other middleware (regardless of its type) and the corresponding view. Django returns the httpresponse immediately.

 

View preprocessing function: process_view (self, request, callback, callback_args, callback_kwargs)

The call time of this method is After Django executes the Request preprocessing function and determines the view to be executed, but before the view function is actually executed.

  • Request: Httprequest object.
  • Callback: Django will call the python function that processes the request. This is the actual function object, not the function name expressed by the string.
  • ARGs:Location Parameter ListBut does not include the request parameter (it is usually the first parameter to pass in the view ).
  • Kwargs: Enter the keyword parameter Dictionary of the view.

Like process_request (), process_view () should return the none or httpresponse object. If none is returned, Django will continue to process this request, execute the subsequent middleware, and then call the corresponding view.

If the httpresponse object is returned, Django will no longer execute any other middleware (regardless of the type) and the corresponding view, and Django will return immediately.

 

Response post-processing function: process_response (self, request, response)

The call time of this method is After Django executes the view function and generates the response.

This processor can modify the content of response. A common purpose is to compress the content, such as the HTML page requested by gzip.

The parameters of this method are quite intuitive: request is the request object, while response is the response object returned from the view.

Process_response () must return the httpresponse object. This response object can be the original object passed into the function (usually modified) or newly generated.

 

Exception post-processing function: process_exception (self, request, exception)

This method is called only when an exception occurs during request processing and the view function throws an uncaptured exception. This Hook can be used to send error notifications, output field information to log files, or even try to automatically recover from errors.

In addition to the consistent request object, the parameters of this function also include the actual exception object exception thrown by the view function.

Process_exception () should return the none or httpresponse object.

If none is returned, Django uses the framework's built-in Exception Handling Mechanism to continue processing the corresponding request.

If the httpresponse object is returned, Django uses this response object, and the short-circuit framework has a built-in exception handling mechanism.

 

[Django] Middleware

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.