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的更多相关文章

  1. Python Django 中间件

    在http请求 到达视图函数之前   和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 中间件的执行流程 1.执行完所有的request方法 到达视图函数. ...

  2. python Django 中间件介绍

    我们一直都在使用中间件,只是没有注意到而已,打开Django项目的Settings.py文件,看到下面的MIDDLEWARE配置项,django默认自带的一些中间件: MIDDLEWARE = [ ' ...

  3. Python - Django - 中间件 process_exception

    process_exception(self, request, exception) 函数有两个参数,exception 是视图函数异常产生的 Exception 对象 process_except ...

  4. Python - Django - 中间件 process_view

    process_view 的执行顺序也是按照 settings.py 中的顺序来执行 process_view 在 urls.py 的对应关系之后,在执行视图函数之前执行 如果返回 None,则继续执 ...

  5. Python - Django - 中间件 process_template_response

    process_template_response(self, request, response) 有两个参数,response 是 TemplateResponse 对象(由视图函数或者中间件产生 ...

  6. Python - Django - 中间件 process_request

    process_request 函数是中间件在收到 request 请求之后执行的函数 该函数的执行顺序是按照 settings.py 中中间件的配置顺序执行的 如果该函数返回 None,继续执行后面 ...

  7. python/ Django之中间件

    python/ Django之中间件 一.中间件 中间件共分为: (1)process_request(self,request) (2)process_view(self, request, cal ...

  8. 【python】-- Django 中间件、缓存、信号

    Django  中间件.缓存.信号 一. Django  中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的 ...

  9. Python自动化之Django中间件

    django中间件 Django请求生命周期 中间件中可以定义方法,分别是 process_request(self,request) process_view(self, request, call ...

随机推荐

  1. 线性排序总结(c++实现)

    前面介绍了一些常用的比较排序算法,它们都是通过比较两个元素的大小进行排序,归并排序和堆排序在最坏情况下的复杂度为O(nlgn),可以证明(使用决策树模型),通过比较进行排序,算法的下界为O(nlgn) ...

  2. python基础语法4 文件处理

    1.什么是文件 操作系统提供给你操作硬盘的一个工具 2.为什么要用文件 因为人类和计算机要永久保存数据 3.怎么用文件 相对路径:a.txt # 必须与当前py文件在同一级目录绝对路径:D:\项目路径 ...

  3. UFUN 函数 UF_DISP (UF_DISP_create_image ) (如何把显示部件部分截图放到指定的文件夹中)

    //此函数功能是输入工作部件的tag,返回工作部件的路径 static string path_name(tag_t path_tag) { ]=""; //得到工作部件的路径 U ...

  4. CentOS7 配置阿里云yum源,vim编辑器,tab自动补全

    1.进入yum的文件夹 命令:cd   /etc/yum.repos.d/ 2.下载wget 命令:yum -y install wget 3.删除yum文件夹所有yum源 命令:rm -rf    ...

  5. vim文本编辑器——替换、保存退出

    1.替换: (1)全文替换: 利用查询命令查询: (2)指定替换的字符串的范围: 2.保存.退出命令: (1)在命令行模式下保存(:w) (2)另存为(:w+要保存的文件的路径) (3)保存退出(:w ...

  6. Vue的SEO问题汇总

    方式一 思否 https://segmentfault.com/q/1010000011824706 SSR 和 Nuxt.js : https://zh.nuxtjs.org/ https://se ...

  7. luoguP3964 [TJOI2013]松鼠聚会

    链接 luogu 思路 切比雪夫距离有max,不好优化. 但是我们能转化成曼哈顿距离,只需要 \((x,y)变成(\frac{x+y}{2},\frac{x-y}{2})\) 相反的曼哈顿距离转切比雪 ...

  8. 【luoguP2989】[USACO10MAR]对速度的需要Need For Speed

    题目描述 最大化平均值 二分一个\(x\) \(check\): \(\frac{F+\sum_{i=1}^{n} X_{i} \times F_{i}}{M+\sum_{i=1}^{n} X_{i} ...

  9. ASP.NET,C#后台调用前台javascript的五种方法

    C#后台调用前台javascript的五种方法 由于项目需要,用到其他项目组用VC开发的组件,在web后台代码无法访问这个组件,所以只好通过后台调用前台的javascript,从而操作这个组件.在网上 ...

  10. 自己动手实现DNS协议

    1. 主要内容 不说废话,直接进入正题.先说说本文本文的主要内容,好让你决定是否看下去: 介绍DNS是干什么的: 介绍DNS是如何工作的: 介绍DNS请求与响应的消息格式: 编程实现一个简单的DNS服 ...