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 ...
随机推荐
- sublime设置默认字体样式
因电脑配置的不同,还有个人喜好的不同,有时候想用自己喜欢的字体来写代码,想用自己习惯的字号大小来显示代码.这些又该怎样设置呢? 本节主要介绍下如何设置字体大小和样式 (1)点菜单栏 “Preferen ...
- oVirt-postgresql
连接数据库 方法一: cd /opt/rh/rh-postgresql95/root/bin su postgres ./psql \c engine 执行sql语句即可 方法二: 用pgAdmin访 ...
- 201671010418 刘佳 实验十四 团队项目评审&课程学习总结
项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十四 团队项目评审&课程学习总结 作业学习目标 (1)掌握软件项目评审会流程: (2)反思总结课程学习内容. Start: ...
- Spring Cloud Config 知识点
Spring Cloud Config项目是一个解决分布式系统的配置管理方案.它包含了Client和Server两个部分,server提供配置文件的存储.以接口的形式将配置文件的内容提供出去,clie ...
- Codeforces Round #603 (Div. 2) A. Sweet Problem(数学)
链接: https://codeforces.com/contest/1263/problem/A 题意: You have three piles of candies: red, green an ...
- MongoDB 聚合函数
概念 聚合函数是对一组值执行计算并返回单一的值 主要的聚合函数 count distinct Group MapReduce 1.count db.users.count() db.users.cou ...
- P1501 [国家集训队]Tree II LCT
链接 luogu 思路 简单题 代码 #include <bits/stdc++.h> #define ls c[x][0] #define rs c[x][1] using namesp ...
- java8 新特性parallelStream 修改默认多线程数量
parallelStream默认使用了fork-join框架,其默认线程数是CPU核心数. 通过测试实践,发现有两种方法来修改默认的多线程数量: 1.全局设置 在运行代码之前,加入如下代码: Syst ...
- C复习---动态内存分配
原型extern void *malloc(unsigned int num_bytes);头文件#include <stdlib.h>#include <malloc.h>函 ...
- yarn一些最佳配置
合理设置队列名 mapreduce.job.queuename设置队列名map读取时进行小文件整合 mapreduce.input.fileinputformat.split.minsize mapr ...