1. Introduction to middleware
1 Introduction
Middleware, as the name suggests, is a processing process between request and response processing. It is relatively lightweight and changes the input and output of django globally. Because the changes are global, you need to be cautious and practical, and improper use will affect performance.
2. Note:
Use middleware for batch processing of all requests
Use decorators when processing certain functions individually
3. Process
When the user initiates a request, it will go through all the middleware in turn. At this time, the request is process_request, and finally reaches the views function. After the views function is processed, it will pass through the middleware in turn. At this time, it is process_response, which is finally returned to the request. By.
4. Flowchart
Django project request data:
User request ------> wsgiref parsing data ------> middleware process_request method ------> urls ------> views view ------> db and template
Django item response data:
views rendering------>urls------> middleware process_response method------>wsgiref package data------>user
2. Use steps
1. First create a folder and write a py file in it
2. Then start writing the class
(1) Middleware is a class, and several methods are written in the class
class M1(MiddlewareMixin): #Must inherit def process_request(self, request): #request: request everything in it print("m1.request_request") #Do not return values easily in this method. If there is a return value, you will not continue to execute the following ones, and execute your own process_response and the response above. # Generally no return value: continue to execute subsequent middleware and view functions def process_response(self, request, response): return response
(2) Add the path to MIDDLEWARE in settings
folder name.py file name.class name
(3) Find the inherited class, and bring that class over
Generally, don't use the imported method, otherwise sometimes there will be no such class after the update, you can take the class it inherits,
3. Example of custom middleware
settings.py file:
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', "app01.utils.mymiddlewares.M1", #Add custom M1 "app01.utils.mymiddlewares.M2" #Add custom M2 ]
Create the utils directory in the app01 directory, and create the mymiddlewares.py file in the utils directory:
mymiddlewares.py file content:
from django.utils.deprecation import MiddlewareMixin from django.shortcuts import HttpResponse class M1(MiddlewareMixin): #Must inherit MiddlewareMixin def process_request(self,request): print("M1 process_request") # if 1: # return HttpResponse("IP ban") #Custom rules can be implemented to ban insecure ip addresses # If there is no return value, continue to execute subsequent middleware and view functions # If there is a return value, execute your own process_response and the above response def process_response(self, request,response): print("M1 process_response") return response class M2(MiddlewareMixin): def process_request(self, request): print("M2 process_request") def process_response(self, request,response): print("M2 process_response") return response #page display index # return HttpResponse("123") #If you replace the response that should have been returned, the last page you see is 123
views.py file content:
from django.shortcuts import render,HttpResponse def index(request): print("index....") return HttpResponse("index")
The order of execution is as follows: