1.用户自定义中间件类时需继承django.utils.deprecation模块的MiddlewareMixin,
process_request(self, request): # 请求时过滤
#request:请求信息, callback:函数名, callback_args:函数可变参数(元组调用), callback_kwargs:函数关键字参数(字典调用)
process_view(self, request, callback, callback_args, callback_kwargs) # 视图函数过滤
process_exception(self, request, exception) # views内代码异常执行,exception:异常信息
process_template_response(self, request, exception) # 如果views返回的对象有render方法 则该函数被执行
process_response(self, request, response): # 返回时过滤
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
网友评论