process_template_response (self, request, response) has two parameters, response is TemplateResponse object (generated by the view function or middleware)
process_template_response function is executed immediately in view of the function is completed
Process_template_response execution function has a precondition that the object have a function to return a view render () method (or indicating that the target object is a TemplateResponse or equivalent method)
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:
Import the render django.shortcuts from, HttpResponse, redirect DEF index (Request): Print ( "This is the index page") REP = HttpResponse ( "Here is the main page index") DEF the render (): Print ( "This is a function where index render method ") return the HttpResponse (" index ") rep.render = render return REP
Visit, http: //127.0.0.1: 8000 / index /
operation result: