Python - Django - 中间件 process_template_response
process_template_response(self, request, response) 有两个参数,response 是 TemplateResponse 对象(由视图函数或者中间件产生)
process_template_response 函数是在视图函数执行完后立即执行的
执行 process_template_response 函数有一个前提条件,那就是视图函数返回的对象要有一个 render() 方法(或者表明该对象是一个 TemplateResponse 对象或等价方法)
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_template_response(self, request, response):
print("这里是 Test 的 process_template_response")
return response class Test2(MiddlewareMixin):
def process_request(self, request):
print("这是一个中间件 --> test2") def process_template_response(self, request, response):
print("这里是 Test2 的 process_template_response")
return response
views.py:
from django.shortcuts import render, HttpResponse, redirect def index(request):
print("这里是 index 页面")
rep = HttpResponse("这里是主页面 index") def render():
print("这里是 index 函数里的 render 方法")
return HttpResponse("index") rep.render = render
return rep
访问,http://127.0.0.1:8000/index/

运行结果:

Python - Django - 中间件 process_template_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_response
process_response 函数是执行完 views.py 后执行的函数 process_response 函数有两个参数,一个是 request,一个是 response,response 是 ...
- 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 ...
随机推荐
- 洛谷 P3071 [USACO13JAN]座位Seating(线段树)
P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...
- centos-限制ssh访问
hosts.allow和hosts.deny规则的执行者为TCP wrappers,对应守护进程为tcpd:而tcpd执行依赖于程序使用了libwrap库. 也就是说:hosts.allow和host ...
- 利用requests库访问网站
1.关于requests库 函数 Response对象包含服务器返回的所有信息,也包含请求的Request信息. 访问百度二十次 import requests def getHTMLText(url ...
- 如何升级pip3
使用pip3安装软件的时候提示要升级pip3“You are using pip version 10.0.1, however version 18.0 is available.You shoul ...
- jmeter,badboy,jar包文件 常数吞吐量计时器?
badboy录制脚本 1.按f2 红色开始录制 URL输入:https://www.so.com/ 2.搜索框输入zxw 回车键搜索 3.选中关键字(刮例如zxw软件——>tools——> ...
- HTML实现调用百度在线翻译API
HTML实现调用百度在线翻译API 本文章已收录于: <!doctype html> <html lang="en"> <head> < ...
- You Can Customize Synthesized Instance Variable Names @property
As mentioned earlier, the default behavior for a writeable property is to use an instance variable c ...
- cpu的发现
system.cpu.discovery 检测到的CPU/CPU内核列表.用于低级发现 返回的cpu从0开始编号,其他关于cpu的监控项就可以使用cpu的id进行单个cpu的资源监控
- 帝国cms替换iwms幻灯图片问题
在管理标签模板中增加一个新模板 页面模板内容为:[!--empirenews.listtemp--]<!--list.var1-->[!--empirenews.listtemp--] 列 ...
- jmeter(四十六)参数化与断言实战
概述 今天用jmeter做一次参数化实战.通过计数器遍历参数表,然后查询jdbc进行beanshell断言. 涉及元件 用户参数,计数器,正则表达式,jdbc,beanshell脚本 详细过程 在我们 ...