中间件的概念
中间件是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出。因为改变的是全局,所以需要谨慎使用,用不好会影响到性能。
如果你想修改请求,例如被传送到view中的HttpRequest对象。或者你想修改view返回的HttpResponse对象,这些都可以通过中间件来实现。
可能你还想在view执行之前做一些操作,这种情况就可以用middleware来实现。
自定义中间件
---process_request,process_response:
当用户发请求的时候会依次经过所有的中间件,这个时候的请求process_request,最后到达views的函数中,views函数处理后,在一次穿过中间件,这个时候是process_response,最后返回给请求者
上述截图中的中间件都是django中的,我们也可以自己定义一个中间件,我们可以自己写一个类,但是必须继承MiddlewareMixin
需要导入:
1 from django.utils.deprecation import MiddlewareMixin
in views:
1 def index(request): 2 3 print("view函数...") 4 return HttpResponse("OK")
in Mymiddlewares.py:
1 from django.utils.deprecation import MiddlewareMixin 2 from django.shortcuts import HttpResponse 3 4 class Md1(MiddlewareMixin): 5 6 def process_request(self,request): 7 print("Md1请求") 8 9 def process_response(self,request,response): 10 print("Md1返回") 11 return response 12 13 class Md2(MiddlewareMixin): 14 15 def process_request(self,request): 16 print("Md2请求") 17 #return HttpResponse("Md2中断") 18 def process_response(self,request,response): 19 print("Md2返回") 20 return response
结果:
1 Md1请求 2 Md2请求 3 view函数... 4 Md2返回 5 Md1返回
流程图:
---process_view:
1 process_view(self, request, callback, callback_args, callback_kwargs)
Mymiddlewares.py:
1 from django.utils.deprecation import MiddlewareMixin 2 from django.shortcuts import HttpResponse 3 4 class Md1(MiddlewareMixin): 5 6 def process_request(self,request): 7 print("Md1请求") 8 #return HttpResponse("Md1中断") 9 def process_response(self,request,response): 10 print("Md1返回") 11 return response 12 13 def process_view(self, request, callback, callback_args, callback_kwargs): 14 print("Md1view") 15 16 class Md2(MiddlewareMixin): 17 18 def process_request(self,request): 19 print("Md2请求") 20 return HttpResponse("Md2中断") 21 def process_response(self,request,response): 22 print("Md2返回") 23 return response 24 25 def process_view(self, request, callback, callback_args, callback_kwargs): 26 print("Md2view")
结果:
1 Md1请求 2 Md2请求 3 Md1view 4 Md2view 5 view函数... 6 Md2返回 7 Md1返回
process_view如果有返回值,会越过其他的process_view以及视图函数,但是所有的process_response都还会执行.
---process_exception:
1 process_exception(self, request, exception)
Mymiddlewares.py:
1 class Md1(MiddlewareMixin): 2 3 def process_request(self,request): 4 print("Md1请求") 5 #return HttpResponse("Md1中断") 6 def process_response(self,request,response): 7 print("Md1返回") 8 return response 9 10 def process_view(self, request, callback, callback_args, callback_kwargs): 11 12 # return HttpResponse("hello") 13 14 # response=callback(request,*callback_args,**callback_kwargs) 15 # return response 16 print("md1 process_view...") 17 18 def process_exception(self): 19 print("md1 process_exception...") 20 21 22 23 class Md2(MiddlewareMixin): 24 25 def process_request(self,request): 26 print("Md2请求") 27 # return HttpResponse("Md2中断") 28 def process_response(self,request,response): 29 print("Md2返回") 30 return response 31 def process_view(self, request, callback, callback_args, callback_kwargs): 32 print("md2 process_view...") 33 34 def process_exception(self): 35 print("md1 process_exception...")
结果:
1 Md1请求 2 Md2请求 3 md1 process_view... 4 md2 process_view... 5 view函数... 6 7 Md2返回 8 Md1返回
流程图:
当views出现错误时: