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 ...
随机推荐
- javascript学习4、Function函数、伪数组arguments
一.Function函数基础 函数:就是将一些语句进行封装,然后通过调用的形式,执行这些语句. 1.函数的作用: 将大量重复的语句写在函数里,以后需要这些语句的时候,可以直接调用函数,避免重复劳动. ...
- list转json数组
lights为arraylist java后台代码: try { org.tempuri.TLight[] lights = phlightSoapProxy.getLights(); ...
- React Virtual DOM Explained in Simple English
If you are using React or learning React, you must have heard of the term “Virtual DOM”. Now what is ...
- (1)IdentityServer4 V3.0.2-安装模板
控制台运行命令: dotnet new -i IdentityServer4.Templates
- RookeyFrame 代码层面 常用方法
测试代码均写在这个类里面的,因为是测试嘛,所以表名那些就将就看了.最后写完了再贴上全部代码 类的路径:Rookey.Frame.Operate.Base -> Test -> Class1 ...
- day03 数据基础
1.列举字符串,列表,元组,字典每个常用的五个方法 字符串: strip() , lstrip(),restrip() count(),index(),find() startswith,endswi ...
- windows cmd编辑文本
echo创建一个空的txt文件:echo.>1.txt这里>表示输出到...echo.表示输出一个空行(即换行)>命令可以扩展为>>表示的意思为附加到...例子:1.tx ...
- 【CSP膜你赛】ATM
题目描述 小沈阳在小品里说过:“人生最痛苦的事情是人死了,钱还没花掉”. 于是小宋(80 岁)决定要将所有的储蓄从 ATM 机中取出花光. 小宋忘记 了她有多少存款(银行卡密码她是记得的 2333), ...
- Hash总结
算法介绍 这个没什么好说的,就是一段比较简单的代码,具体的话要看题目. 自然溢出 这个是比较好用而且容易被卡的一种Hash方式. 用\(2^{64}\)当模数然后做\(Hash\),常数比较小. 单模 ...
- DNS基本操作详解
在很多人看来,DNS只是为外部提供DNS解析服务(我以前也是这么认为的,直到膝盖中了一箭),但作为互联网的基础设施,DNS远没有想象的那么简单.如果你没有听说过DNS查询.反向解析.zone传输.动态 ...