1. middleware简介
Django的middleware的概念相当于SSH框架里面的filter的概念。中间键的作用就是对所有的request,在request前,和在response后做一定的处理。
Django的中间键类型分为五种:
请求(Request)中间件->对应函数process_request
视图(View)中间件->对应函数process_view
模板(Template)中间件->对应函数process_template_response(不常用)
响应(Response)中间件->对应函数process_response
异常(Exception)中间件->对应函数process_exception(不常用)
2. middleware中间键函数的执行顺序和过程
(1)请求(Request)中间件和响应(Response)中间件的正常执行顺序
接下来就用程序演示:
m1.py
from django.utils.deprecation import MiddlewareMixin class ROW1(MiddlewareMixin):
def process_request(self,request):
print("朱三")
def process_response(self,request,response):
print('赵六')
return response class ROW2(MiddlewareMixin):
def process_request(self,request):
print("李四") def process_response(self,request,response):
print('薛七')
return response class ROW3(MiddlewareMixin):
def process_request(self,request):
print("王五")
def process_response(self,request,response):
print('宋八')
return response
views.py
def test(request):
# int("asdsadsad")
print('小猪->没带钱')
return HttpResponse("OK")
所以顺序为:朱三-->李四-->王五-->小猪-->没带钱-->宋八-->薛七-->赵六
如果在中间件中发生错误,那么其执行顺序为
m1.py
from django.utils.deprecation import MiddlewareMixin class ROW1(MiddlewareMixin):
def process_request(self,request):
print("朱三")
def process_response(self,request,response):
print('赵六')
return response from django.shortcuts import HttpResponse
class ROW2(MiddlewareMixin):
def process_request(self,request):
print("李四")
return HttpResponse('离开') def process_response(self,request,response):
print('薛七')
return response class ROW3(MiddlewareMixin):
def process_request(self,request):
print("王五") def process_response(self,request,response):
print('宋八')
return response
views与上面一样,执行顺序为:朱三-->李四-->薛七-->赵六
(2)视图中间件执行顺序
from django.utils.deprecation import MiddlewareMixin class ROW1(MiddlewareMixin):
def process_request(self,request):
print("朱三") def process_response(self,request,response):
print('赵六')
return response def process_view(self, request, veiw_func, veiw_func_args, veiw_func_kwargs):
print('张欣彤') from django.shortcuts import HttpResponse
class ROW2(MiddlewareMixin):
def process_request(self,request):
print("李四") def process_response(self,request,response):
print('薛七')
return response def process_view(self, request, veiw_func, veiw_func_args, veiw_func_kwargs):
print('张需要') class ROW3(MiddlewareMixin):
def process_request(self,request):
print("王五") def process_response(self,request,response):
print('宋八') return response
def process_view(self, request, veiw_func, veiw_func_args, veiw_func_kwargs):
print('邵林')
views.py代码与其最上的views.py代码相同
则执行顺序为:朱三-->李四-->王五-->张欣彤-->张需要-->邵林-->小猪-->没带钱-->宋八-->薛七-->赵六
而模板中间件(函数中出现render才生效)和异常中间价(函数出现错误才生效)因为不常用,默认不生效,所以就不详细介绍了。
3.自定义中间件
(1)创建中间件
class RequestExeute(object):
def process_request(self,request):
pass
def process_view(self, request, callback, callback_args, callback_kwargs):
i =1
pass
def process_exception(self, request, exception):
pass
def process_response(self, request, response):
return response
(2)注册中间件
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'wupeiqi.middleware.auth.RequestExeute',
)