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 ...
随机推荐
- Beta冲刺博客汇总(校园帮-追光的人)
所属课程 软件工程1916 作业要求 Beta冲刺博客汇总 团队名称 追光的人 作业目标 汇总Beta阶段的博客,方便查看 冲刺日志 Beta之前-凡事预则立(校园帮-追光的人)5-22 Beta冲刺 ...
- 34、使用Python操作MySql数据库和MsSql数据库
一.数据库的安装和连接 1. PyMySQL的安装 pip install pymysql 2 .python连接数据库 import pymysql db = pymysql.connect(&qu ...
- 查看spark on yarn的日志和程序状态的方法
转自:https://blog.csdn.net/high2011/article/details/52132646 一.在命令行使用命令查看 (1)查看日志:yarn logs -applicati ...
- js 复选框回显
<div class="control-group"> <label class="control-label">客户状态:</l ...
- Educational Codeforces Round 47 (Rated for Div. 2) G. Allowed Letters
把原字符看成 $X$,每个位置看成 $Y$,每种字符向每个能去的位置连边,就成了一个二分图完美匹配的问题.现要得到字典序最小,那么就枚举每一位要放什么,然后看放完这种字符,剩下的字符的个数和后面能不能 ...
- Good Article Good sentence HDU - 4416 (后缀自动机)
Good Article Good sentence \[ Time Limit: 3000 ms\quad Memory Limit: 32768 kB \] 题意 给出一个 \(S\) 串,在给出 ...
- re.sub 实现多处替换
1 | 表示或的意思 将所有字母替换掉 result_content = re.sub('a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z', ...
- Java实现PV操作 | 哲学家进餐问题
运行结果: Java代码: public class Main { public static void main(String[] args) { Global global=new Global( ...
- cf1173 D. Nauuo and Circle
链接 [cf]http://codeforces.com/contest/1175/problem/F) 思路 当1在1的位置做dp[i]为i的子树所有的方案. 一条性质是i的子树所占圆上的位置一定一 ...
- exit命令
exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行. 常用参数格式:exit n退出.设置退出码为n.(Cause the shell to exit with a statu ...