django中间件介绍
在学习django中间件之前,先来认识一下django的生命周期,如下图所示:
django生命周期:浏览器发送的请求会先经过wsgiref模块处理解析出request(请求数据)给到中间件,然后通过路由控制执行对应的视图函数,从而和模板,db进行交互,交互完的数据再通过视图函数返回给中间件,最后wsgiref模块将返回的数据封装成http形式的数据给到浏览器并进行展示。
了解了django的生命周期后,我们就可以开始着手写一个自己的中间件了,接下来认识几个常用的中间件方法
1.process_request
单个中间件
首先在app下创建一个py文件,定义你的中间件类名
from django.utils.deprecation import MiddlewareMixin from django.http import HttpResponse class MiddlewareShow(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow1')
然后将你的py文件路径写入django主项目的settings的MIDDLEWARE中
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', 'proxy_pro.middleware.MiddlewareShow' # 此条是新加的 从该项目路径开始写 ]
然后执行一个视图函数即可,查看控制台打印
此时自己创建的process_request方法就生效了
多个中间件
此时在之前的py文件中再新建一个类
class MiddlewareShow(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow1 Request') class MiddlewareShowTwo(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow2 Request')
然后将新建类的路径也放在django主项目的settings的MIDDLEWARE中
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', 'proxy_pro.middleware.MiddlewareShow', 'proxy_pro.middleware.MiddlewareShowTwo' ]
执行视图函数,查看控制台打印
此时定义的两个中间件都生效了,执行顺序是先1后2
2.process_response
分别在刚才的类中添加response方法
from django.utils.deprecation import MiddlewareMixin from django.http import HttpResponse class MiddlewareShow(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow1 Request') def process_response(self, request, response): print('MiddlewareShow1 Response') return response class MiddlewareShowTwo(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow2 Request') def process_response(self, request, response): print('MiddlewareShow2 Response') return response
执行视图函数,查看控制台打印
此时可以看出请求先依次执行了中间件的Request,然后再去执行视图函数,返回是先执行跟后面的中间件再依次往前
这时突然冒出一个想法,如果在request时直接返回了某个东西,还会继续去执行后面的视图函数吗?那我们就来测试一下
from django.utils.deprecation import MiddlewareMixin from django.http import HttpResponse class MiddlewareShow(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow1 Request') return HttpResponse('request1时已返回') def process_response(self, request, response): print('MiddlewareShow1 Response') return response class MiddlewareShowTwo(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow2 Request') def process_response(self, request, response): print('MiddlewareShow2 Response') return response
执行视图函数,查看控制台
此时可以看出当request中有返回时,直接不执行后面的内容了
3.process_view
分别在刚才的py文件中添加view方法
from django.utils.deprecation import MiddlewareMixin from django.http import HttpResponse class MiddlewareShow(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow1 Request') def process_response(self, request, response): print('MiddlewareShow1 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow1 process_view') class MiddlewareShowTwo(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow2 Request') def process_response(self, request, response): print('MiddlewareShow2 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow2 process_view')
执行视图函数,查看控制台返回
此时可以看出这个process_view方法会在执行完request后执行,执行完再去执行视图函数
这时候就有疑问了,那这个有什么用呢?我们先把它里面的参数打印一下
from django.utils.deprecation import MiddlewareMixin from django.http import HttpResponse class MiddlewareShow(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow1 Request') def process_response(self, request, response): print('MiddlewareShow1 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow1 process_view') print('=====>callback', callback) print('=====>callback_args', callback_args) class MiddlewareShowTwo(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow2 Request') def process_response(self, request, response): print('MiddlewareShow2 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow2 process_view')
执行视图函数打印结果
此时可以看到这个view里面的参数callback是视图方法,callback_args是请求参数,那我们试着去请求下看看
class MiddlewareShow(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow1 Request') def process_response(self, request, response): print('MiddlewareShow1 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow1 process_view') print('=====>callback', callback) print('=====>callback_args', callback_args) ret = callback(callback_args) # 请求 return ret class MiddlewareShowTwo(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow2 Request') def process_response(self, request, response): print('MiddlewareShow2 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow2 process_view')
查看打印结果
结果显然易见,请求视图函数成功了,返回了以后就没有去执行process_view2了,这边的作用就是可以拦截请求
4.process_exception
分别在刚才的py文件中添加exception方法
from django.utils.deprecation import MiddlewareMixin from django.http import HttpResponse class MiddlewareShow(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow1 Request') def process_response(self, request, response): print('MiddlewareShow1 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow1 process_view') # print('=====>callback', callback) # print('=====>callback_args', callback_args) # ret = callback(callback_args) # return ret def process_exception(self, request, exception): print('MiddlewareShow1 process_exception') return HttpResponse(exception) class MiddlewareShowTwo(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow2 Request') def process_response(self, request, response): print('MiddlewareShow2 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow2 process_view') def process_exception(self, request, exception): print('MiddlewareShow2 process_exception') return HttpResponse(exception)
执行视图函数,查看控制台
问题来了,为啥没有走这个中间件方法呢?别慌,我们在视图函数中加个错
def middle_show(request): leo print('执行了视图函数') return HttpResponse('hhh')
此时在执行下看看
此时可以看到当process_exception2获取到报错后,就返回了没有执行process_exception1
那如果我们在process_exception1处理呢,我们来测试下
from django.utils.deprecation import MiddlewareMixin from django.http import HttpResponse class MiddlewareShow(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow1 Request') def process_response(self, request, response): print('MiddlewareShow1 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow1 process_view') # print('=====>callback', callback) # print('=====>callback_args', callback_args) # ret = callback(callback_args) # return ret def process_exception(self, request, exception): print('MiddlewareShow1 process_exception') return HttpResponse(exception) class MiddlewareShowTwo(MiddlewareMixin): def process_request(self, request): print('MiddlewareShow2 Request') def process_response(self, request, response): print('MiddlewareShow2 Response') return response def process_view(self, request, callback, callback_args, callback_kwargs): print('MiddlewareShow2 process_view') def process_exception(self, request, exception): print('MiddlewareShow2 process_exception')
执行视图函数,打印看下
此时还会走到process_exception1哦,然后把错误返回给页面。
以上就是django中间件的介绍,希望和大家多多学习!转载请说明出处,尊重劳动成果!!!