1.用戶自定義中間件類時(shí)需繼承django.utils.deprecation模塊的MiddlewareMixin累榜,
process_request(self, request): # 請(qǐng)求時(shí)過濾
#request:請(qǐng)求信息, callback:函數(shù)名, callback_args:函數(shù)可變參數(shù)(元組調(diào)用), callback_kwargs:函數(shù)關(guān)鍵字參數(shù)(字典調(diào)用)
process_view(self, request, callback, callback_args, callback_kwargs) # 視圖函數(shù)過濾
process_exception(self, request, exception) # views內(nèi)代碼異常執(zhí)行,exception:異常信息
process_template_response(self, request, exception) # 如果views返回的對(duì)象有render方法 則該函數(shù)被執(zhí)行
process_response(self, request, response): # 返回時(shí)過濾
2.MIDDLEWARE 中添加 # 'login.middlewares.MD1',
login 為app name
middlewares 為 middlewares.py
MD1 為function
3.middlewares.py
from django.utils.deprecation import MiddlewareMixin
class MD1(MiddlewareMixin):
def process_response(self,request, response):
#訪問的路徑
print(request.path)
return response