码迷,mamicode.com
首页 > 其他好文 > 详细

django中间件 process_view

时间:2017-09-22 22:34:15      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:epo   ges   elf   callback   roc   9.png   images   alt   return   

先执行所有的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 response
class 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 response
class 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

技术分享

 

技术分享

 

django中间件 process_view

标签:epo   ges   elf   callback   roc   9.png   images   alt   return   

原文地址:http://www.cnblogs.com/tiantao36/p/7577118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!