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 from django.utils.deprecation import MiddlewareMixin from django.shortcuts import redirect,HttpResponse,render class Md1(MiddlewareMixin): def process_request(self,request): print("Md1请求") def process_response(self,request,response): print("Md1返回") return response class Md2(MiddlewareMixin): def process_request(self,request): print("Md2请求") def process_response(self,request,response): print("Md2返回") return response
接着我们按照顺序分别加入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函数
def index(request): print("view视图") return HttpResponse("执行了view函数")
执行结果:(并且页面正常显示)
Md1请求
Md2请求
view视图
Md2返回
Md1返回
现在我们在request的后面加上一个 return
class Md1(MiddlewareMixin): def process_request(self,request): print("Md1请求") return Httpresponse("Md1中断") def process_response(self,request,response): print("Md1返回") return response class Md2(MiddlewareMixin): def process_request(self,request): print("Md2请求") return Httpresponse("Md2中断") def process_response(self,request,response): print("Md2返回") return response
结果
Md1请求
Md1返回
#页面显示
Md1中断 ----》也就是没有执行views里面的函数
当两个中间件只有request和response的情况
1 request没有return的情况,按照顺序
Md1 request ----> Md2 request 将view函数返回给用户 --->Md2 response --->Md1 response
2 request有return的情况:
Md1 request ----> 查看有没有return,有的话返回给用户----->执行 Md1 response
没有return -------》Md2 request 查看有没有return,有的话返回给用户 ---->执行Md2 response ---> Md1 response
没有return ,将view函数返回给用户 ---》执行Md2 response ---> Md1 response
2 两个中间件,有 request response view
没有return的情况
from django.utils.deprecation import MiddlewareMixin from django.shortcuts import redirect,HttpResponse,render class Md1(MiddlewareMixin): def process_request(self,request): print("Md1请求") # return HttpResponse("Md1中断") #返回到前端 def process_response(self,request,response): print("Md1返回") return response #不加return会报错 def process_view(self,request,callback,callback_args,callback_kwargs): print("Md1 process_view",callback,callback_args,callback_kwargs) class Md2(MiddlewareMixin): def process_request(self,request): print("Md2请求") # return HttpResponse("Md2中断") def process_response(self,request,response): print("Md2返回") return response def process_view(self,request,callback,callback_args,callback_kwargs): print("Md2 process_view",callback,callback_args,callback_kwargs)
结果:
Md1请求
Md2请求
Md1 process_view <function index at 0x000001B6C0A336A8> () {}
Md2 process_view <function index at 0x000001B6C0A336A8> () {}
view视图
Md2返回
Md1返回
有return的情况
def process_view(self,request,callback,callback_args,callback_kwargs): print("Md1 process_view",callback,callback_args,callback_kwargs) ret=callback(request) return ret def process_view(self,request,callback,callback_args,callback_kwargs): print("Md2 process_view",callback,callback_args,callback_kwargs) ret=callback(request) #执行views里面的函数 --------------------------》 callback(request) 相当于执行 views里面的函数 return HttpResponse(ret)
结果:Md2的 process_view没有执行
Md1请求
Md2请求
Md1 process_view <function index at 0x000001F8AA3436A8> () {}
view视图
Md2返回
Md1返回
两个中间件 含有 request response view
Md1中process_view有return的话,不执行 Md2的process_view
没有return和return在Md2的话,依次执行 Md1 process_view ---> Md2 process_view ---->view函数
无论有无return 返回给用户的都是views视图里面的函数
3 两个中间件 含有 request response view exception
process_exception是当执行发生错误时,返回给用户一个东西
没有return的情况:
from django.utils.deprecation import MiddlewareMixin from django.shortcuts import redirect,HttpResponse,render class Md1(MiddlewareMixin): def process_request(self,request): print("Md1请求") def process_response(self,request,response): print("Md1返回") return response #不加return会报错 def process_view(self,request,callback,callback_args,callback_kwargs): print("Md1 process_view",callback,callback_args,callback_kwargs) def process_exception(self,request,exception): print("md1 process_exception") class Md2(MiddlewareMixin): def process_request(self,request): print("Md2请求") def process_response(self,request,response): print("Md2返回") return response def process_view(self,request,callback,callback_args,callback_kwargs): print("Md2 process_view",callback,callback_args,callback_kwargs) def process_exception(self,request,exception): print("md2 process_exception")
结果:exception的内容没有打印
Md1请求
Md2请求
Md1 process_view <function index at 0x00000292CCBA46A8> () {}
Md2 process_view <function index at 0x00000292CCBA46A8> () {}
view视图
Md2返回
Md1返回
含有return的情况
from django.shortcuts import redirect,HttpResponse,render class Md1(MiddlewareMixin): def process_request(self,request): print("Md1请求") def process_response(self,request,response): print("Md1返回") return response #不加return会报错 def process_view(self,request,callback,callback_args,callback_kwargs): print("Md1 process_view",callback,callback_args,callback_kwargs) def process_exception(self,request,exception): print("md1 process_exception") return HttpResponse(exception) class Md2(MiddlewareMixin): def process_request(self,request): print("Md2请求") def process_response(self,request,response): print("Md2返回") return response def process_view(self,request,callback,callback_args,callback_kwargs): print("Md2 process_view",callback,callback_args,callback_kwargs) def process_exception(self,request,exception): print("md2 process_exception") return HttpResponse(exception)
结果和没有return的时候一模一样,也就时exception里面的命令没有执行
我们现在认为的制造一个错误
def index(request): print("view视图") print(xinxin) #这里是一个错误 return HttpResponse("执行view函数")
都有return的情况
Md1请求
Md2请求
Md1 process_view <function index at 0x00000186896336A8> () {}
Md2 process_view <function index at 0x00000186896336A8> () {}
view视图
md2 process_exception #执行了 Md2的exception,并且页面返回的是 return HttpResponse(exception)
Md2返回
Md1返回
Md2没有return,Md1有return的情况
Md1请求
Md2请求
Md1 process_view <function index at 0x00000276983B36A8> () {}
Md2 process_view <function index at 0x00000276983B36A8> () {}
view视图
md2 process_exception
md1 process_exception
Md2返回
Md1返回
exception总结:
当页面没有发生错误的时候,有无return,有多少个return都不会执行 exception里面的命令
当页面有错误时:倒着从excepton中找return 找到了就返回给用户 【Md2执行excepition,没有return的话,执行 Md1中的exception,有返回给用户,都没有的话,页面报错】
return HttpResponse(exception):中 exception存储了报错内容
最最最最后:
process_request 是顺序执行的
process_view 是顺序执行的
process_exception 是倒着查找的
process_response 是倒着执行的
希望本文所述对大家基于Django框架的Python程序设计有所帮助。