标签:方法 函数 port cat 名称 roc 机制 import print
1.中间件的方法
process_request(self,request) process_view(self, request, callback, callback_args, callback_kwargs) process_template_response(self,request,response) process_exception(self, request, exception) process_response(self, request, response
2.中间件运行机制
a.玩法1
from django.utils.deprecation import MiddlewareMixin class M1(MiddlewareMixin): def process_request(self, request): print(‘M1.request‘) def process_view(self, request,callback,callback_args,callback_kwargs ): print("M1.process_view") def process_response(self, request, response): print(‘M1.response‘) return response class M2(MiddlewareMixin): def process_request(self, request): print(‘M2.request‘) def process_view(self, request,callback,callback_args,callback_kwargs ): print("M2.process_view") def process_response(self, request, response): print(‘M2.response‘) return response
#### 输出结果 M1.request M2.request M1.process_view M2.process_view #执行views视图 M2.response M1.response
b.玩法2
from django.utils.deprecation import MiddlewareMixin class M1(MiddlewareMixin): def process_request(self, request): print(‘M1.request‘) # callback视图函数名称 callback_args,callback_kwargs 视图函数执行所需的参数 def process_view(self, request,callback,callback_args,callback_kwargs ): print("M1.process_view") response=callback(request,*callback_args,**callback_kwargs) return response def process_response(self, request, response): print(‘M1.response‘) return response class M2(MiddlewareMixin): def process_request(self, request): print(‘M2.request‘) def process_view(self, request,callback,callback_args,callback_kwargs ): print("M2.process_view") def process_response(self, request, response): print(‘M2.response‘) return response
####输出结果 M1.request M2.request M1.process_view #执行views视图函数 M2.response M1.response
标签:方法 函数 port cat 名称 roc 机制 import print
原文地址:https://www.cnblogs.com/konglingxi/p/9408365.html