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

  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_response

    process_response 函数是执行完 views.py 后执行的函数 process_response 函数有两个参数,一个是 request,一个是 response,response 是 ...

  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. Linux系统下jar包的启动方式

    Linux 运行jar包命令如下: 方式一: Java -jar shareniu.jar 特点:当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,程序退出 那如何让窗口不锁定? ...

  2. nuxt 项目设置缩进为4个空格

    1..editorconfig 文件下的indent_size: 2更改为indent_size: 4 2..prettierrc 文件 { "singleQuote": true ...

  3. dart 中的 try on catch

    catch 捕获异常 捕获了一个异常后,就停止了捕获异常过程.捕获一个异常,你就有机会去处理它: try { breedMoreLlamas(); } on OutOfLlamasException ...

  4. wordpress时间函数the_time() 实例解读

    wordpress the_time()时间函数想必大家多多少少都会用到,但是要自定义一些时间相对没那么熟悉了,随ytkah一起来看看吧.我们知道时间函数基础调用是<?php the_time( ...

  5. There is no type initializer in Swift----One answer is to use static, it is the same as class final.

    “Unlike stored instance properties, you must always give stored type properties a default value. Thi ...

  6. reids 数据库学习

    最近项目中用到了redis数据库,学习整理下 redis操作学习手册--key操作命令 http://www.cnblogs.com/stephen-liu74/archive/2012/03/26/ ...

  7. 3.深入学习Servlet的Response和Request

    一.HttpServletResponse web服务器接受到客户端的HTTP请求,对于这个请求分别创建一个代表请求的对象HttpServletRequest和一个代表响应的对象HttpServlet ...

  8. SDU暑假排位第一场 (Gym - 100889)

    啊今天有点挂机啊 D题和队友暴力后发现一组数据跑得飞快 然后遇上1e5组数据就没了..... 然后我疯狂优化暴力 然后去世了 最后半小时F也没写出来 主要还是最后有点慌并且没有考虑清楚 导致情况越写越 ...

  9. A1102 | 反转二叉树

    #include <stdio.h> #include <memory.h> #include <math.h> #include <string> # ...

  10. 【题解】洛谷 P1449 后缀表达式

    目录 题目 思路 \(Code\) 题目 P1449 后缀表达式 思路 栈.题目说的不是很清楚,没说包含什么操作.除法用整数除法就行. 先string读入字符串,然后从前往后看如果是个数字就入栈,如果 ...