django框架中间件原理与用法详解!
本文实例讲述了django框架中间件原理与用法。分享给大家供大家参考,具体如下:
中间件:轻量级,介于request和response之间的一道处理过程,在全局上改变了输入和输出
在django中就默认使用了七个中间件
MIDDLEWARE=[ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
通过我们查看django的源码可以知道,中间件的使用方法有如下几种
process_request(self,request) process_view(self,request,callback,callback_args,callbackkwargs) process_template_response(self,request,response) process_exception(self,request,exception) process_response(self,request,response)
下面我们来通过自定义中间件来理解它们之间的顺序
1最基本的一个中间件有两种方法:process_request process_response
在project下面写一个py文件作为中间件文件
#my_middleware.py fromdjango.utils.deprecationimportMiddlewareMixin fromdjango.shortcutsimportredirect,HttpResponse,render classMd1(MiddlewareMixin): defprocess_request(self,request): print("Md1请求") defprocess_response(self,request,response): print("Md1返回") returnresponse classMd2(MiddlewareMixin): defprocess_request(self,request): print("Md2请求") defprocess_response(self,request,response): print("Md2返回") returnresponse
接着我们按照顺序分别加入settings
MIDDLEWARE=[ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'my_middleware.Md1', 'my_middleware.Md2', ]
定义一个views函数
defindex(request): print("view视图") returnHttpResponse("执行了view函数")
执行结果:(并且页面正常显示)
Md1请求
Md2请求
view视图
Md2返回
Md1返回
现在我们在request的后面加上一个return
classMd1(MiddlewareMixin): defprocess_request(self,request): print("Md1请求") returnHttpresponse("Md1中断") defprocess_response(self,request,response): print("Md1返回") returnresponse classMd2(MiddlewareMixin): defprocess_request(self,request): print("Md2请求") returnHttpresponse("Md2中断") defprocess_response(self,request,response): print("Md2返回") returnresponse
结果
Md1请求
Md1返回
#页面显示
Md1中断 ----》也就是没有执行views里面的函数
当两个中间件只有request和response的情况
1request没有return的情况,按照顺序
Md1request ----> Md2request将view函数返回给用户--->Md2response--->Md1response
2request有return的情况:
Md1request ---->查看有没有return,有的话返回给用户----->执行Md1response
没有return-------》Md2request查看有没有return,有的话返回给用户---->执行Md2response --->Md1response
没有return,将view函数返回给用户 ---》执行Md2response --->Md1response
2两个中间件,有request response view
没有return的情况
fromdjango.utils.deprecationimportMiddlewareMixin fromdjango.shortcutsimportredirect,HttpResponse,render classMd1(MiddlewareMixin): defprocess_request(self,request): print("Md1请求") #returnHttpResponse("Md1中断")#返回到前端 defprocess_response(self,request,response): print("Md1返回") returnresponse#不加return会报错 defprocess_view(self,request,callback,callback_args,callback_kwargs): print("Md1process_view",callback,callback_args,callback_kwargs) classMd2(MiddlewareMixin): defprocess_request(self,request): print("Md2请求") #returnHttpResponse("Md2中断") defprocess_response(self,request,response): print("Md2返回") returnresponse defprocess_view(self,request,callback,callback_args,callback_kwargs): print("Md2process_view",callback,callback_args,callback_kwargs)
结果:
Md1请求
Md2请求
Md1process_view(){}
Md2process_view(){}
view视图
Md2返回
Md1返回
有return的情况
defprocess_view(self,request,callback,callback_args,callback_kwargs): print("Md1process_view",callback,callback_args,callback_kwargs) ret=callback(request) returnret defprocess_view(self,request,callback,callback_args,callback_kwargs): print("Md2process_view",callback,callback_args,callback_kwargs) ret=callback(request)#执行views里面的函数--------------------------》callback(request)相当于执行views里面的函数 returnHttpResponse(ret)
结果:Md2的process_view没有执行
Md1请求
Md2请求
Md1process_view(){}
view视图
Md2返回
Md1返回
两个中间件含有request response view
Md1中process_view有return的话,不执行Md2的process_view
没有return和return在Md2的话,依次执行Md1process_view --->Md2process_view ---->view函数
无论有无return返回给用户的都是views视图里面的函数
3两个中间件含有request response view exception
process_exception是当执行发生错误时,返回给用户一个东西
没有return的情况:
fromdjango.utils.deprecationimportMiddlewareMixin fromdjango.shortcutsimportredirect,HttpResponse,render classMd1(MiddlewareMixin): defprocess_request(self,request): print("Md1请求") defprocess_response(self,request,response): print("Md1返回") returnresponse#不加return会报错 defprocess_view(self,request,callback,callback_args,callback_kwargs): print("Md1process_view",callback,callback_args,callback_kwargs) defprocess_exception(self,request,exception): print("md1process_exception") classMd2(MiddlewareMixin): defprocess_request(self,request): print("Md2请求") defprocess_response(self,request,response): print("Md2返回") returnresponse defprocess_view(self,request,callback,callback_args,callback_kwargs): print("Md2process_view",callback,callback_args,callback_kwargs) defprocess_exception(self,request,exception): print("md2process_exception")
结果:exception的内容没有打印
Md1请求
Md2请求
Md1process_view(){}
Md2process_view(){}
view视图
Md2返回
Md1返回
含有return的情况
fromdjango.shortcutsimportredirect,HttpResponse,render classMd1(MiddlewareMixin): defprocess_request(self,request): print("Md1请求") defprocess_response(self,request,response): print("Md1返回") returnresponse#不加return会报错 defprocess_view(self,request,callback,callback_args,callback_kwargs): print("Md1process_view",callback,callback_args,callback_kwargs) defprocess_exception(self,request,exception): print("md1process_exception") returnHttpResponse(exception) classMd2(MiddlewareMixin): defprocess_request(self,request): print("Md2请求") defprocess_response(self,request,response): print("Md2返回") returnresponse defprocess_view(self,request,callback,callback_args,callback_kwargs): print("Md2process_view",callback,callback_args,callback_kwargs) defprocess_exception(self,request,exception): print("md2process_exception") returnHttpResponse(exception)
结果和没有return的时候一模一样,也就时exception里面的命令没有执行
我们现在认为的制造一个错误
defindex(request): print("view视图") print(xinxin)#这里是一个错误 returnHttpResponse("执行view函数")
都有return的情况
Md1请求
Md2请求
Md1process_view(){}
Md2process_view(){}
view视图
md2process_exception #执行了Md2的exception,并且页面返回的是returnHttpResponse(exception)
Md2返回
Md1返回
Md2没有return,Md1有return的情况
Md1请求
Md2请求
Md1process_view(){}
Md2process_view(){}
view视图
md2process_exception
md1process_exception
Md2返回
Md1返回
exception总结:
当页面没有发生错误的时候,有无return,有多少个return都不会执行exception里面的命令
当页面有错误时:倒着从excepton中找return 找到了就返回给用户【Md2执行excepition,没有return的话,执行Md1中的exception,有返回给用户,都没有的话,页面报错】
returnHttpResponse(exception):中exception存储了报错内容
最最最最后:
process_request是顺序执行的
process_view 是顺序执行的
process_exception是倒着查找的
process_response 是倒着执行的
希望本文所述对大家基于Django框架的Python程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。