第十五章 中间件
- 中间件:
-
是一个用来处理django请求和响应的框架级别的钩子,它是一个轻量、低级别的插件系统,用于在全局范围内改变Django的输入和输出。
-
每个中间件组件都负责做一些特定的功能。
-
本质上是一个类。
-
request对象是wsgi封装进行传递给django
-
五个方法,四个特征(执行时间和顺序,参数,返回值)
-
中间件的方法可以选择性使用,中间件的类需要继承MiddlewareMixin
-
中间件可以放于任意位置,需要在settings中进行注册,具体到中间件类名
# 注册中间件, settings.py MIDDLEWARE=[ 'app01.mymiddleware.middleware.Md1', 'app01.mymiddleware.middleware.Md2', ]
# app01.mymiddleware中的middleware.py # 导入类 from django.shortscut import HttpResponse from django.utils.deprecation import MiddlewareMixin
1. process_request()
-
参数:self, request
# md1中间件 class Md1(MiddlewareMixin): def process_request(self, request): print('This is Md1 process_request') # print(id(request)) # return HttpResponse("here's process_request method of Md1") # md2中间件 class Md2(MiddlewareMixin): def process_request(self, request): print('This is Md2 process_request, Md2') # print(id(request)) # return HttpResponse("here's process_request method of Md2")
-
执行时间:视图函数之前
-
参数:request(最终传递给view函数) 和视图函数是同一个request对象
-
执行顺序:按照注册的顺序执行
-
返回值:
-
为None的情况是正常流程
-
返回HttpResponse,执行当前process_request方法,倒序执行其他中间件的process_response方法,响应给浏览器
-
2. process_view()
-
参数:self, request,view_func, view_args, view_kwargs
1. 示例
# md1中间件 class Md1(MiddlewareMixin): def process_view(self, *args, **kwargs): print('This is Md1 process_view') # md2中间件 class Md2(MiddlewareMixin): def process_view(self, *args, **kwargs): print('This is Md2 process_view, Md2')
2. 特征
-
执行时间:
-
参数:
-
request和视图函数是同一个request对象
-
view_func:视图函数(路由匹配得到的视图函数)
-
view_args:视图的位置参数(路由匹配时的分组)
-
view_kwargs:视图的关键字参数
-
-
执行顺序:按照注册的顺序执行
-
返回值:
-
为None的情况是正常流程
-
HttpResponse,执行当前process_view方法,倒序执行最后中间件process_view方法,响应给浏览器
-
3. process_response()
-
参数:self, request, response
-
必须有返回值,response
1. 示例
# Md1中间件 class Md1(MiddlewareMixin): def process_request(self, request): print('This is Md1 process_request') # print(id(request)) # return HttpResponse("here's process_request method of Md1") def process_view(self, *args, **kwargs): print('This is Md1 process_view') def process_response(self, request, response): print('This is Md1 process_response') return response # Md2中间件 class Md2(MiddlewareMixin): def process_request(self, request): print('This is Md2 process_request, Md2') # print(id(request)) # return HttpResponse("here's process_request method of Md2") def process_view(self, *args, **kwargs): print('This is Md2 process_view, Md2') def process_response(self, request, response): print('This is Md2 process_response, Md2') return response
2. 特征
-
执行时间:视图函数之后
-
参数:
-
request和视图函数是同一个request对象
-
response返回给浏览器的的响应对象
-
-
执行顺序:按照注册的倒序执行
-
返回值:
-
response处理完成后必须返回response对象
-
4. prcess_exeception()
-
参数:self, request, exception
1. 示例
from django.http import HttpResponse from django.utils.deprecation import MiddlewareMixin class Md1(MiddlewareMixin): def process_request(self, request): print('This is Md1 process_request') # print(id(request)) # return HttpResponse("here's process_request method of Md1") def process_view(self, *args, **kwargs): print('This is Md1 process_view') def process_response(self, request, response): print('This is Md1 process_response') return response def process_exception(self, request, exception): print('This is Md1 process_response') print(exception) return HttpResponse('错误') class Md2(MiddlewareMixin): def process_request(self, request): print('This is Md2 process_request, Md2') # print(id(request)) # return HttpResponse("here's process_request method of Md2") def process_view(self, *args, **kwargs): print('This is Md2 process_view, Md2') def process_response(self, request, response): print('This is Md2 process_response, Md2') return response def process_exception(self, request, exception): print('This is Md2 process_response, Md2') print(exception) return HttpResponse('错误,Md2')
-
触发条件:视图层面有错误,视图函数之后
-
参数:
-
request和视图函数是同一个request对象
-
exception:捕获的异常对象
-
-
执行顺序:按照注册的倒序执行
-
返回值:
-
为None时,交给下一个中间件继续处理,如果都有,则交由django处理
-
如果有返回值,则继续倒序执行process_response方法
-
5. process_template_response()
-
参数 :self, request, response
1. 示例
# mymiddleware.py from django.http import HttpResponse from django.utils.deprecation import MiddlewareMixin class Md1(MiddlewareMixin): def process_template_response(self, request, response): print("here's process_template_response of Md1") # 渲染的字典数据 response.context_data['name'] = 'henry' return response class Md2(MiddlewareMixin): def process_template_response(self, request, response): print("here's process_template_response of Md2") # 需要渲染的模版名 response.template_name='test.html' return response
# viwes.py from django.shortcuts import render from django.template.response import TemplateResponse def index(request): print('here is index') return TemplateResponse(request, 'index.html', {'name': 'herny', 'age': 19})
2. 特征
-
-
参数:
-
request和视图函数是同一个request对象
-
response:templateResponse对象
-
-
执行顺序:按照注册的倒序执行
-
返回值:
-
必须返回HttpResponse对象,即return reponse
-