中间件
Posted mlhz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了中间件相关的知识,希望对你有一定的参考价值。
1 中间件的概念
中间件是介于request与response处理之间的一道处理过程,因为改变的时全局,所有需要谨慎使用。
2 django默认的Middleware:
1 MIDDLEWARE = [ 2 ‘django.middleware.security.SecurityMiddleware‘, 3 ‘django.contrib.sessions.middleware.SessionMiddleware‘, 4 ‘django.middleware.common.CommonMiddleware‘, 5 ‘django.middleware.csrf.CsrfViewMiddleware‘, 6 ‘django.contrib.auth.middleware.AuthenticationMiddleware‘, 7 ‘django.contrib.messages.middleware.MessageMiddleware‘, 8 ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘, 9 ]
每一个中间件都有具体的方法
3 自定义中间件
中间件中一共有四种方法
1 process_request 2 3 process_view 4 5 6 process_exception 7 8 9 process_response
4 process_request,process_response
当用户发起请求的时候会依次经过所有的中间件,这个时候的请求时的process_request,最后达到views的函数中,views函数处理后,会依次经过中间件,这个时候时process_response,最后返回给请求者
上面的截图的中间件都是在django中的,我们也可以自己定义中间件,我们可以写一个类,但是必须继承MiddlewareMixin
需要导入:from django.utils.deprecation import MiddlewareMixin
in views:
1 def index(request): 2 print(‘‘vvvv‘) 3 return HttpResponse(‘ok‘)
in Mymiddlewares.py:
1 from django.untils.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 21 22 23 24 结果: 25 Mdl 请求 26 Md2请求 27 view函数 28 Md2返回 29 Md1 返回
注意:如果当请求到达请求2时直接不符合条件时返回,程序将把请求直接发给中间件2返回,然后依次返回到请求者,结果如下:
Md1请求
Md2请求
Md2返回
Md1返回
5 process_view: 参数如下
process_views(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")
结果如下:
Md1请求
Md2请求
Md1view
Md2view
view函数...
Md2返回
Md1返回
下图进行分析上面的过程:
当最后一个中间的process_request到达路由关系映射之后,返回到中间件1的process_view,然后依次往下,到达view函数,最后通过process_response依次返回到用户
process_view可以用来调用视图函数:
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
注意:process_view如果有返回值,会越过其他的process_view以及视图函数,但是所有的process_response都还会执行。
6 process_exception
process_exception(
self
, request, exception)
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...")
以上是关于中间件的主要内容,如果未能解决你的问题,请参考以下文章
从没有中间转换变量的片段中观察 ViewModel LiveData