Django--中间件
Posted 大亮头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django--中间件相关的知识,希望对你有一定的参考价值。
1、原理
在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法。
请求到来时,在wsgi和urls中间有一层中间件,准确的说是4层方法和Django框架的处理:
process_request(self,request)
process_view(self, request, callback, callback_args, callback_kwargs)
views
process_exception(self, request, exception)
process_response(self, request, response)
这些函数的名称和参数不能改变,任何一个环节具有Response,就会交给process_response()处理,从而返回给用户。
2、自定义中间件
创建package
middle1.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class mmm( object ): def process_request( self ,request): print "mmm.process_request" def process_view( self ,request,callback,callback_args,callback_kwargs): print "mmm.process_view" def process_response( self ,request,response): print "mmm.process_response" return response class xxx( object ): def process_request( self ,request): print "xxx.process_request" def process_view( self ,request,callback,callback_args,callback_kwargs): print "xxx.process_view" def process_response( self ,request,response): print "xxx.process_response" return response |
settings.py
1 2 3 4 5 6 7 8 9 10 11 12 | MIDDLEWARE_CLASSES = [ ‘django.middleware.security.SecurityMiddleware‘ , ‘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‘ , ‘middleware.middle1.mmm‘ , ‘middleware.middle1.xxx‘ , ] |
views.py
1 2 3 | def index(request): print "view" return HttpResponse( "index" ) |
IDE
可以看到执行顺序,注意的是,process_response()的执行顺序是从后往前的。
3、源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #Django自动去settings里找 MIDDLEWARE_CLASSES process_request_list = [] process_view_list = [] process_response_list = [] process_exception_list = [] for class in MIDDLEWARE_CLASSES: obj = class () if hasattr (obj,process_request): process_request_list.append(obj.process_request) if hasattr (obj,process_view): process_view_list.append(obj.process_view) if hasattr (obj,process_response): process_response_list.append(obj.process_response) if hasattr (obj,process_exception): process_exception_list.append(obj.process_exception) for i in process_request_list: i() for i in process_view_list: i() #Django渲染 for i in process_exception_list: i() for i in process_response_list: i() |
4、process_exception
views.py
1 2 | def index(request): raise Exception( "报错了" ) |
middleware/middle.py
1 2 3 4 5 6 7 8 9 10 11 | class mmm( object ): def process_request( self ,request): print "mmm.process_request" def process_view( self ,request,callback,callback_args,callback_kwargs): print "mmm.process_view" def process_response( self ,request,response): print "mmm.process_response" return response def process_exception( self , request, exception): print exception return request |
browser
IDE
注意:?
process_request和process_view方法是按自定义中间件的顺序执行
process_exception和process_response是按中间件从后往前的顺序执行
以上是关于Django--中间件的主要内容,如果未能解决你的问题,请参考以下文章
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段
Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段