Django中间件
在http请求 到达视图函数之前 和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法。 中间件的执行流程 1、执行完所有的request方法 到达视图函数。 2、执行中间件的其他方法 3、经过所有response方法 返回客户端。 注意:如果在其中1个中间件里 request方法里 return了值,就会执行当前中间件的response方法,返回给用户 然后 报错。。不会再执行下一个中间件。自定义中间件
1.在project下随便创建一个py文件from django.utils.deprecation import MiddlewareMixin class Middle1(MiddlewareMixin): def process_request(self,request): print("来了") def process_response(self, request,response): print('走了')
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', 'M1.Middle1', ]
View Code
中间件(类)中5种方法
中间件中可以定义5个方法,分别是:- process_request(self,request)
- process_view(self, request, callback, callback_args, callback_kwargs)
- process_template_response(self,request,response)
- process_exception(self, request, exception)
- process_response(self, request, response
from django.utils.deprecation import MiddlewareMixin class M1(MiddlewareMixin): def process_request(self, request): print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M1.process_view") def process_response(self, request, response): print('M1.response') return response class M2(MiddlewareMixin): def process_request(self, request): print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M2.process_view") def process_response(self, request, response): print('M2.response') return response
from django.utils.deprecation import MiddlewareMixin class M1(MiddlewareMixin): def process_request(self, request): print('M1.request') # callback视图函数名称 callback_args,callback_kwargs 视图函数执行所需的参数 def process_view(self, request,callback,callback_args,callback_kwargs ): print("M1.process_view") response=callback(request,*callback_args,**callback_kwargs) return response def process_response(self, request, response): print('M1.response') return response class M2(MiddlewareMixin): def process_request(self, request): print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M2.process_view") def process_response(self, request, response): print('M2.response') return response
from django.utils.deprecation import MiddlewareMixin class M1(MiddlewareMixin): def process_request(self, request): print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M1.process_view") def process_response(self, request, response): print('M1.response') return response def process_exception(self, request,exception): print('M1的process_exception') class M2(MiddlewareMixin): def process_request(self, request): print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M2.process_view") def process_response(self, request, response): print('M2.response') return response def process_exception(self, request, exception): print('M2的process_exception')
M1.request M2.request M1.process_view M2.process_view 执行index M2的process_exception M1的process_exception Internal Server Error: /index/ Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 41, in inner response = get_response(request) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "F:\untitled1\app01\views.py", line 7, in index int("ok") ValueError: invalid literal for int() with base 10: 'ok' M2.response M1.response [03/Jul/2017 16:43:59] "GET /index/ HTTP/1.1" 500 62663
M1.request M2.request M1.process_view M2.process_view 执行index M2的process_exception (有了return值,直接执行response) M2.response M1.response
class M1(MiddlewareMixin): def process_request(self, request): print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M1.process_view") def process_response(self, request, response): print('M1.response') return response def process_exception(self, request,exception): print('M1的process_exception') class M2(MiddlewareMixin): def process_request(self, request): print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M2.process_view") def process_response(self, request, response): print('M2.response') return response def process_exception(self, request, exception): print('M2的process_exception') return HttpResponse('出错了兄弟!!!')
from django.utils.deprecation import MiddlewareMixin from django.shortcuts import HttpResponse class M1(MiddlewareMixin): def process_request(self, request): print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M1.process_view") def process_response(self, request, response): print('M1.response') return response def process_exception(self, request,exception): print('M1的process_exception') class M2(MiddlewareMixin): def process_request(self, request): print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M2.process_view") def process_response(self, request, response): print('M2.response') return response def process_exception(self, request, exception): print('M2的process_exception') def process_template_response(self,request,response): print('M2process_template_response') return response
from django.utils.deprecation import MiddlewareMixin from django.shortcuts import HttpResponse class M1(MiddlewareMixin): def process_request(self, request): print('M1.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M1.process_view") def process_response(self, request, response): print('M1.response') return response def process_exception(self, request,exception): print('M1的process_exception') class M2(MiddlewareMixin): def process_request(self, request): print('M2.request') def process_view(self, request,callback,callback_args,callback_kwargs ): print("M2.process_view") def process_response(self, request, response): print('M2.response') return response def process_exception(self, request, exception): print('M2的process_exception') def process_template_response(self,request,response): #如果视图函数中的返回值 中有render方法,才会执行 process_template_response print('M2process_template_response') return response
from django.shortcuts import render,HttpResponse # Create your views here. class Foo(): def __init__(self,requ): self.req=requ def render(self): return HttpResponse('OKKKK') def index(request): print("执行index") obj=Foo(request) return obj
from django.shortcuts import render,HttpResponse # Create your views here. class Dict(): #对视图函数返回值做二次封装 !! def __init__(self,requ,msg): self.req=requ self.msg=msg def render(self): a=self.msg #在render方法里面 把视图函数的 返回值 制作成字典 、列表等。。。 # 如果新增了其他 一个视图函数直接,return对象 即可!不用每个视图函数都写 制作字典 列表 拼接的逻辑了 return HttpResponse(a) # def index(request): print("执行index") obj=Dict(request,"vv") return obj