django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法。
在django项目的settings模块中,有一个 MIDDLEWARE_CLASSES 变量,其中每一个元素就是一个中间件,如下图。
与mange.py在同一目录下的文件夹 wupeiqi/middleware下的auth.py文件中的Authentication类
中间件中可以定义四个方法,分别是:
- 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)
以上方法的返回值可以是None和HttpResonse对象,如果是None,则继续按照django定义的规则向下执行,如果是HttpResonse对象,则直接将该对象返回给用户。
自定义中间件
1、settings.py
from django.middleware.common import CommonMiddleware
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',
'md.M1',
'md.M2',
]
2、views.py
from django.shortcuts import render,HttpResponse,redirect # Create your views here. def test(request): print("test===") return HttpResponse("...666")
3、新建md.py
#!/usr/bin/env python # -*- coding:utf8 -*- # from django.middleware.common import CommonMiddleware 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") def process_response(self,request,response): print("m1.process_request") 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") response = callback(request,*callback_args,**callback_kwargs) return response def process_response(self,request,response): print("m2.process_response") return response
3.1 views.py 抓取异常
#!/usr/bin/env python # -*- coding:utf8 -*- # from django.middleware.common import CommonMiddleware 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") def process_response(self, request, response): print("m1.process_request") return response def process_exception(self, request, exception): print("m1.process_exception") 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") # response = callback(request,*callback_args,**callback_kwargs) # return response def process_response(self,request,response): print("m2.process_response") return response def process_exception(self,request,exception): print("m2.process_exception") return HttpResponse('错拉...')
3.1.1
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') # response = callback(request,*callback_args,**callback_kwargs) # return response 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): """ 视图函数的返回值中,如果有render方法,才被调用 :param request: :param response: :return: """ 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') def process_response(self,request,response): print('m2.process_response') return response def process_exception(self, request, exception): print('m2.process_exception') return HttpResponse('错误了...')