The use of Django middleware??

Source: Internet
Author: User

In Django's middleware (middleware), in Django, the middleware is actually a class, and after the request arrives and ends, Django executes the appropriate method in the middleware at the right time according to its own rules.

In the settings module of the Django Project, there is a middleware_classes variable, each of which is a middleware, such as.



Authentication class in auth.py file under folder Wupeiqi/middleware under the same directory as mange.py

Five methods can be defined in a middleware, namely:

1 process_request (self,request) 2 Process_view (self, request, callback, Callback_args, Callback_kwargs) 3 process_template_response (self,request,response) 4 process_exception (self, request, exception) 5 Process_response (self, request, response)

Django pre-provisioned six methods in the middleware, the difference between the six methods is different stages of execution, the input or output to dry
Pre-method as follows:

1. Initialize: No parameters are required, the server is called once in response to the first request and is used to determine whether the current middleware is enabled.

1. def __init__ (): Pass

2. Before processing the request: called on each request, returns None or the HttpResponse object.

1. def process_request (Request): Pass

3. Before the view is processed: called on each request, returns None or HttpResponse object

1. def Process_view (Request, View_func, View_args, View_kwargs): Pass

4. Before processing the template response: called on each request, returns the response object that implements the Render method.

1. def Process_template_response (Request, response): Pass

5. After processing the response: All responses are called before returning to the browser, and are called on each request, returning the HttpResponse object.

1. def Process_response (Request, response): Pass

6. Exception handling: Called when the view throws an exception, called on each request, returns a HttpResponse object.

1. def process_exception (request,exception): Pass

The use of 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.