1. middleware簡介
Django的middleware的概念相當於SSH框架里面的filter的概念。中間鍵的作用就是對所有的request,在request前,和在response后做一定的處理。
Django的中間鍵類型分為五種:
請求(Request)中間件->對應函數process_request
視圖(View)中間件->對應函數process_view
模板(Template)中間件->對應函數process_template_response(不常用)
響應(Response)中間件->對應函數process_response
異常(Exception)中間件->對應函數process_exception(不常用)
2. middleware中間鍵函數的執行順序和過程
(1)請求(Request)中間件和響應(Response)中間件的正常執行順序

接下來就用程序演示:
m1.py
from django.utils.deprecation import MiddlewareMixin class ROW1(MiddlewareMixin): def process_request(self,request): print("朱三")
def process_response(self,request,response): print('趙六') return response class ROW2(MiddlewareMixin): def process_request(self,request): print("李四") def process_response(self,request,response): print('薛七') return response class ROW3(MiddlewareMixin): def process_request(self,request): print("王五")
def process_response(self,request,response): print('宋八') return response
views.py
def test(request): # int("asdsadsad") print('小豬->沒帶錢') return HttpResponse("OK")
所以順序為:朱三-->李四-->王五-->小豬-->沒帶錢-->宋八-->薛七-->趙六
如果在中間件中發生錯誤,那么其執行順序為

m1.py
from django.utils.deprecation import MiddlewareMixin class ROW1(MiddlewareMixin): def process_request(self,request): print("朱三") def process_response(self,request,response): print('趙六') return response from django.shortcuts import HttpResponse class ROW2(MiddlewareMixin): def process_request(self,request): print("李四") return HttpResponse('離開') def process_response(self,request,response): print('薛七') return response class ROW3(MiddlewareMixin): def process_request(self,request): print("王五") def process_response(self,request,response): print('宋八') return response
views與上面一樣,執行順序為:朱三-->李四-->薛七-->趙六
(2)視圖中間件執行順序

from django.utils.deprecation import MiddlewareMixin class ROW1(MiddlewareMixin): def process_request(self,request): print("朱三") def process_response(self,request,response): print('趙六') return response def process_view(self, request, veiw_func, veiw_func_args, veiw_func_kwargs): print('張欣彤') from django.shortcuts import HttpResponse class ROW2(MiddlewareMixin): def process_request(self,request): print("李四") def process_response(self,request,response): print('薛七') return response def process_view(self, request, veiw_func, veiw_func_args, veiw_func_kwargs): print('張需要') class ROW3(MiddlewareMixin): def process_request(self,request): print("王五") def process_response(self,request,response): print('宋八') return response def process_view(self, request, veiw_func, veiw_func_args, veiw_func_kwargs): print('邵林')
views.py代碼與其最上的views.py代碼相同
則執行順序為:朱三-->李四-->王五-->張欣彤-->張需要-->邵林-->小豬-->沒帶錢-->宋八-->薛七-->趙六
而模板中間件(函數中出現render才生效)和異常中間價(函數出現錯誤才生效)因為不常用,默認不生效,所以就不詳細介紹了。
3.自定義中間件
(1)創建中間件
class RequestExeute(object): def process_request(self,request): pass def process_view(self, request, callback, callback_args, callback_kwargs): i =1 pass def process_exception(self, request, exception): pass def process_response(self, request, response): return response
(2)注冊中間件
MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'wupeiqi.middleware.auth.RequestExeute', )
