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??