Python - Django - 中间件 process_response
process_response 函数是执行完 views.py 后执行的函数
process_response 函数有两个参数,一个是 request,一个是 response,response 是视图函数返回的响应对象
process_response 函数的返回值必须是 HttpResponse 对象
middleware_test.py:
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse class Test(MiddlewareMixin):
def process_request(self, request):
print("这是一个中间件 --> test") def process_response(self, request, response):
print("这里是 Test 的 HttpResponse")
return HttpResponse("这是 Test 返回的 HttpResponse") class Test2(MiddlewareMixin):
def process_request(self, request):
print("这是一个中间件 --> test2") def process_response(self, request, response):
print("这里是 Test2 的 HttpResponse")
return HttpResponse("这是 Test2 返回的 HttpResponse")
views.py:
from django.shortcuts import HttpResponse def index(request):
print("这里是 index 页面")
return HttpResponse("这里是主页面 index")
访问,http://127.0.0.1:8000/index/

这里没有返回 index 函数中的内容,而是直接返回 process_response 中的内容
执行结果:

可以看到先执行 process_request,然后执行 views.py,最后执行 process_response
而且 process_request 按照中间件的顺序执行,而 process_response 是反着执行的
process_request 和 process_response 的执行流程:
中间件收到 request 请求后,先执行 process_request 函数,当返回响应时,再执行 process_response 函数

当 process_request 函数返回 HttpResponse 时,就不再执行后面的 process_request 函数,而会去执行 process_response 函数
如上图,中间件 3 返回了 HttpResponse,所以不再执行中间件 4、5、6 的 process_request 函数和 process_response 函数,而是继续执行中间件 3、2、1 的 process_response 函数
middleware_test.py:
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse allow_url = ['/admin/', '/uploads/', '/files/'] class Test(MiddlewareMixin):
def process_request(self, request):
print("这是一个中间件 --> test") def process_response(self, request, response):
print("这里是 Test 的 HttpResponse")
return HttpResponse("这是 Test 返回的 HttpResponse") class Test2(MiddlewareMixin):
def process_request(self, request):
print("这是一个中间件 --> test2")
if request.path_info in allow_url:
return
else:
return HttpResponse("--- Test2 ---") def process_response(self, request, response):
print("这里是 Test2 的 HttpResponse")
return HttpResponse("这是 Test2 返回的 HttpResponse")
访问,http://127.0.0.1:8000/index/

运行结果:

因为 Test2 中 process_request 中返回了 HttpResponse,所以这里只执行了 Test2 的 process_response 方法,而不再去执行后面的中间件 Test1
Python - Django - 中间件 process_response的更多相关文章
- Python Django 中间件
在http请求 到达视图函数之前 和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 中间件的执行流程 1.执行完所有的request方法 到达视图函数. ...
- python Django 中间件介绍
我们一直都在使用中间件,只是没有注意到而已,打开Django项目的Settings.py文件,看到下面的MIDDLEWARE配置项,django默认自带的一些中间件: MIDDLEWARE = [ ' ...
- Python - Django - 中间件 process_exception
process_exception(self, request, exception) 函数有两个参数,exception 是视图函数异常产生的 Exception 对象 process_except ...
- Python - Django - 中间件 process_view
process_view 的执行顺序也是按照 settings.py 中的顺序来执行 process_view 在 urls.py 的对应关系之后,在执行视图函数之前执行 如果返回 None,则继续执 ...
- Python - Django - 中间件 process_template_response
process_template_response(self, request, response) 有两个参数,response 是 TemplateResponse 对象(由视图函数或者中间件产生 ...
- Python - Django - 中间件 process_request
process_request 函数是中间件在收到 request 请求之后执行的函数 该函数的执行顺序是按照 settings.py 中中间件的配置顺序执行的 如果该函数返回 None,继续执行后面 ...
- python/ Django之中间件
python/ Django之中间件 一.中间件 中间件共分为: (1)process_request(self,request) (2)process_view(self, request, cal ...
- 【python】-- Django 中间件、缓存、信号
Django 中间件.缓存.信号 一. Django 中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的 ...
- Python自动化之Django中间件
django中间件 Django请求生命周期 中间件中可以定义方法,分别是 process_request(self,request) process_view(self, request, call ...
随机推荐
- PAT 乙级 1008.数组元素循环右移问题 C++/Java
1008 数组元素循环右移问题 (20 分) 题目来源 一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A0A1⋯ ...
- 2016年的EK工具
什么是Exploit Kit? 预打包了安装程序.控制面板.恶意代码以及相当数量的攻击工具.通常基于PHP的一个软件. Exploit Kit经济体制 价格在成百上千美元: 可以按日/周/月来付租金: ...
- 《快活帮》第八次团队作业:Alpha冲刺
项目 内容 这个作业属于哪个课程 2016计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8-软件测试与ALPHA冲刺 团队名称 快活帮 作业学习目标 (1)掌握 ...
- mysql 的 3306、33060 端口区别
Port 3306 is the default port for the MySQL Protocol, which is used by the mysql client, MySQL Conne ...
- 27.SpringBoot和SpringMVC的区别
所以,用最简练的语言概括就是: Spring 是一个“引擎”: Spring MVC 是基于Spring的一个 MVC 框架: Spring Boot 是基于Spring4的条件注册的一套快速开发整合 ...
- php面试题收藏
总结几个要素: 1.个人简介名字大写,内容需要详实,一是可以给人留下映像,二是减少不必要的与面试官交换个人信息的时间.准备一份好的口头自我介绍是很有必要的,毕竟准备一次能用很久,时间花在上面很实用,面 ...
- 08_MSTP(数通华为)
1. 网络拓扑 2. SW1配置[SW1]vlan batch 10 20 30 40[SW1]stp mode mstp 进入MSTP配置视图,MSTP域名为huawei,同时配置VLAN到实例的映 ...
- 再谈System.BadImageFormatException
今天,当我们继续学习.NET异常处理系列时,我们将查看System.BadImageFormatException.System.BadImageFormatException与GIF或JPG无关,而 ...
- webpack配合babel使用
一.babel介绍 ①Babel 是一个 JavaScript 编译器,可以把ES6的语法转为兼容浏览器的ES5语法 ②Babel中文官网:https://www.babeljs.cn/ ③Babel ...
- ACM数据结构-树状数组
模板: int n; int tree[LEN]; int lowbit(int x){ return x&-x; } void update(int i,int d){//index,del ...