先执行所有的request
在执行所有的view
在执行reponse
url匹配路由
class M1(MiddlewareMixin): def process_request(self, request): print(‘m1.process_request‘) def process_view(self, request, callback, callback_args, callback_kwargs): """ 如果有返回值,则不在继续执行,直接到最后一个中间件的response """ print(‘m1.process_view‘, callback) # return HttpResponse(‘Process View返回值‘) def process_response(self,request, response): print(‘m1.prcess_response‘) return responseclass M2(MiddlewareMixin): def process_request(self, request): print(‘m2.process_request‘) def process_view(self, request, callback, callback_args, callback_kwargs): """ 如果有返回值,则不在继续执行,直接到最后一个中间件的response """ print(‘m2.process_view‘, callback) # return HttpResponse(‘Process View返回值‘) def process_response(self,request, response): print(‘m2.prcess_response‘) return response
m1.process_request
m2.process_request
m1.process_view <function LoginView at 0x0000000003B531E0>
m2.process_view <function LoginView at 0x0000000003B531E0>
m2.prcess_response
m1.prcess_response
callback视图函数名
从哪里可以知道是怎么执行的,看process_view。
process_view能不能有返回值 如果proces——view有返回值,后面的proces_view就不执行了
class M1(MiddlewareMixin): def process_request(self, request): print(‘m1.process_request‘) def process_view(self, request, callback, callback_args, callback_kwargs): """ 如果有返回值,则不在继续执行,直接到最后一个中间件的response """ print(‘m1.process_view‘, callback) return HttpResponse(‘Process View返回值‘) def process_response(self,request, response): print(‘m1.prcess_response‘) return responseclass M2(MiddlewareMixin): def process_request(self, request): print(‘m2.process_request‘) def process_view(self, request, callback, callback_args, callback_kwargs): """ 如果有返回值,则不在继续执行,直接到最后一个中间件的response """ print(‘m2.process_view‘, callback) # return HttpResponse(‘Process View返回值‘) def process_response(self,request, response): print(‘m2.prcess_response‘) return response结果:
m1.process_request
m2.process_request
m1.process_view <function LoginView at 0x0000000003B631E0>
m2.prcess_response
m1.prcess_response
时间: 2024-10-28 15:07:33