django 中间件
################################中间件的几种函数###############################
###########process-view###############
from django.utils.deprecation import MiddlewareMixin
class M1(MiddlewareMixin):
def process_request(self,request):
print("m1.process-request")
def process_view(self,request,callback,callback_args,callback_kwargs):
print("m1.process-view")
return callback(request,*callback_args,**callback_kwargs)
def process_response(self,request,response):
print("m1.process-response")
return response
def process_exception(self,request,exception):
print("m1.process-exception")
def process_template_response(self,request,response):
print("m1.process-template-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):
print("m2.process-view")
return callback(request,*callback,**callback_kwargs)
def process_response(self,request,response):
print("m2.process-response")
return response
def process_exception(self,request,exception):
print("m2.process-exception")
def process_template_response(self,request,response):
print("m2.process-template-response")
return response
正常执行的执行结果: 1、执行request,按照注册顺序来执行,一只执行到最后一个中间件函数,取到视图函数中的函数
2、然后再按照注册顺序执行,如果中间件中有view的话,就直接将此函数执行
3、之后直接不执行后面中间件的view函数,跳到最后一个中间件的response,然后倒序返回。
m1.process-request
m2.process-request
m1.process-view
m2.process-response
m1.process-response
###########process-exception###############
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
class M1(MiddlewareMixin):
def process_request(self,request):
print("m1.process-request")
def process_view(self,request,callback,callback_args,callback_kwargs):
print("m1.process-view")
# return callback(request,*callback_args,**callback_kwargs)
def process_response(self,request,response):
print("m1.process-response")
return response
def process_exception(self,request,exception):
print("m1.process-exception")
return HttpResponse("出错了!!!")
def process_template_response(self,request,response):
print("m1.process-template-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):
print("m2.process-view")
# return callback(request,*callback,**callback_kwargs)
def process_response(self,request,response):
print("m2.process-response")
return response
def process_exception(self,request,exception):
print("m2.process-exception")
# return HttpResponse("出错了!!!")
def process_template_response(self,request,response):
print("m2.process-template-response")
return response
结果: 以下结果:如果异常在m2的process-exception中有处理异常的话,就直接不执行m1的exception了。
m1.process-request
m2.process-request
m1.process-view
m2.process-view
m2.process-exception
m1.process-exception
m2.process-response
m1.process-response
###########process-template-response###############
class JSONResponse:
def __init__(self,req,status,msg):
self.req = req
self.status = status
self.msg = msg
def render(self):
import json
ret = {
'status': self.status,
'msg':self.msg
}
return HttpResponse(json.dumps(ret))
def test(request):
# print('test')
# return HttpResponse('ok')
ret = {}
return JSONResponse(request,True,"错误信息") #视图函数的返回值中(对象),如果它有render方法,它才会被调用。
结果: 整个执行流程如此
m1.process-request
m2.process-request
m1.process-view
m2.process-view
m2.process-template-response
m1.process-template-response
m2.process-response
m1.process-response