中間件是介於request與response處理之間的一道處理過程,相對比較輕量級,而且在全局上改變django的輸入與輸出。由於改變的是全局,因此須要謹慎使用,用很差會影響到性能。django
Django中中間件定義:服務器
Middleware is a framework of hooks into Django’s request/response processing. <br>It’s a light, low-level 「plugin」 system for globally altering Django’s input or output.
若是你想修改請求,例如被傳送到view中的HttpRequest對象。 或者你想修改view返回的HttpResponse對象,可能你還想在view執行以前作一些操做,這些均可以經過中間件來實現。session
Django默認的Middleware(每一箇中間件都有具體的功能):函數
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', ]
中間件中一共有四個方法:性能
process_request
process_view
process_exception
process_response
當用戶發起請求時,會依次通過全部的中間件方法process_request,而後到達views的函數中,views函數處理後,再依次穿過中間件方法process_response。最後返回給請求者。spa
自定義中間件,能夠寫一個類,可是必須繼承MiddlewareMixincode
from django.utils.deprecation import MiddlewareMixin
views.py:csrf
def index(request): print("view函數") return HttpResponse("OK")
Mymiddlewares.py:中間件
from django.utils.deprecation import MiddlewareMixin from django.shortcuts import HttpResponse class Md1(MiddlewareMixin): def process_request(self, request): print("Md1請求") def process_response(self, request, response): print("Md1返回") return response class Md2(MiddlewareMixin): def process_request(self, request): print("Md2請求") #return HttpResponse("Md2中斷") def process_response(self, request, response): print("Md2返回") return response
結果:對象
Md1請求
Md2請求
view函數...
Md2返回
Md1返回
當請求到達請求2時直接不符合條件返回,即return HttpResponse("Md2中斷"),程序將把請求直接發給中間件2返回,而後依次返回到請求者,結果以下:
Md1請求
Md2請求
Md2返回
Md1返回
此時,流程圖以下:
Mymiddlewares.py:
from django.utils.deprecation import MiddlewareMixin from django.shortcuts import HttpResponse class Md1(MiddlewareMixin): def process_request(self, request): print("Md1請求") #return HttpResponse("Md1中斷") def process_response(self, request, response): print("Md1返回") return response def process_view(self, request, callback, callback_args, callback_kwargs): print("Md1view") class Md2(MiddlewareMixin): def process_request(self, request): print("Md2請求") #return HttpResponse("Md2中斷") def process_response(self, request, response): print("Md2返回") return response def process_view(self, request, callback, callback_args, callback_kwargs): print("Md1view")
結果:
Md1請求
Md2請求
Md1view
Md2view
view函數...
Md2返回
Md1返回
此時,流程圖以下:
當最後一箇中間的process_request到達路由關係映射以後,返回到中間件1的process_view,而後依次往下,到達views函數,最後經過process_response依次返回到達用戶。
當process_view若是有返回值,會越過其餘的process_view以及視圖函數,可是全部的process_response都還會執行。
當視圖函數出現錯誤時,則會依次各中間件的process_exception。但若是某一中間件process_exception函數有返回值時,則越過剩下未執行的中間件process_exception函數,但全部的process_response都還會執行。
Mymiddlewares.py:
class Md1(MiddlewareMixin): def process_request(self,request): print("Md1請求") #return HttpResponse("Md1中斷") def process_response(self,request,response): print("Md1返回") return response def process_view(self, request, callback, callback_args, callback_kwargs): # return HttpResponse("hello") # response=callback(request,*callback_args,**callback_kwargs) # return response print("md1 process_view...") def process_exception(self): print("md1 process_exception...") class Md2(MiddlewareMixin): def process_request(self,request): print("Md2請求") # return HttpResponse("Md2中斷") def process_response(self,request,response): print("Md2返回") return response def process_view(self, request, callback, callback_args, callback_kwargs): print("md2 process_view...") def process_exception(self): print("md1 process_exception...")
結果:
Md1請求
Md2請求
md1 process_view...
md2 process_view...
md2 process_exception
md1 process_exception
Md2返回
Md1返回
此時,流程圖以下:
將md2的process_exception修改以下:
def process_exception(self,request,exception): print("md2 process_exception...") return HttpResponse("error")
此時結果以下:
Md1請求
Md2請求
md1 process_view...
md2 process_view...
md2 process_exception...
Md2返回
Md1返回
某些IP訪問服務器的頻率太高,進行攔截,好比限制每份鐘不能超過20次
若是用戶訪問的是login視圖(放過)
若是訪問其餘視圖,須要檢測是否是有session認證,如時有則放行,沒有返回login頁面,這樣就免得在多個視圖函數上寫裝飾器了。