1, Middleware definition
Official statement: middleware is a framework level hook used to process Django's requests and responses. It is a lightweight, low-level plug-in system for changing Django's input and output globally.
Each middleware component is responsible for some specific functions. However, because it affects the overall situation, it needs to be used with caution. Improper use will affect performance.
To put it bluntly, middleware helps us to do some additional operations before and after the view function is executed. It is essentially a user-defined class, in which several methods are defined,
The Django framework executes these methods at a specific time of the request.
We have been using MIDDLEWARE all the time, but we didn't notice it. Open the settings of Django project Py file, see the midview configuration item in the figure below.
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
The midview configuration item is a list in which strings are actually classes, that is, MIDDLEWARE.
Have we touched a csrf related middleware before? At the beginning, we asked everyone to comment it out, and then submit the post request, it will not be forbidden,
Later learned to use CSRF_ The middleware will not be annotated after token. Next, learn the methods in middleware and when they are executed.
2, Custom Middleware
Middleware can define five methods: process_request and process_response
- process_request(self,request)
- process_view(self, request, view_func, view_args, view_kwargs)
- process_template_response(self,request,response)
- process_exception(self, request, exception)
- process_response(self, request, response)
The return value of the above method can be None or an HttpResponse object. If it is None, continue to execute backward according to the rules defined by django. If it is an HttpResponse object, directly return the object to the user.
1. Customize a middleware example
from django.utils.deprecation import MiddlewareMixin class MD1(MiddlewareMixin): def process_request(self, request): print("MD1 Inside process_request") def process_response(self, request, response): print("MD1 Inside process_response") return response
2,process_request
process_request has a parameter, request, which is the same as the request in the view function.
Its return value can be None or HttpResponse object. If the return value is None, continue according to the normal process and hand it over to the next middleware for processing. If it is an HttpResponse object,
Django will not execute the view function, but return the corresponding object to the browser. Let's take a look at how Django executes the process in multiple middleware_ Of the request method.
from django.utils.deprecation import MiddlewareMixin class MD1(MiddlewareMixin): def process_request(self, request): print("MD1 Inside process_request") class MD2(MiddlewareMixin): def process_request(self, request): print("MD2 Inside process_request") pass
stay
settings.py to register the above two custom MIDDLEWARE in the midview configuration item:
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'middlewares.MD1', # Custom middleware MD1 'middlewares.MD2' # Custom middleware MD2 ]
At this time, we visit a view and find that the following contents are printed in the terminal:
MD1 Inside process_request MD2 Inside process_request app01 Medium index view
Change the positions of MD1 and MD2, and then access a view. You will find that the contents printed in the terminal are as follows:
MD2 Inside process_request MD1 Inside process_request app01 Medium index view