Answer the question
In order to leave comments, you need to log in
How in django python, using the middleware, to change the content of the page before it is displayed by render in the view?
https://docs.djangoproject.com/en/2.1/topics/http/...
The reference information indicates which part of the code is executed before and which after view .
But nothing is said about how to change the generated content of the page before it is displayed after view .
class SimpleMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
class MyMiddleware(object):
def __init__(self, get_response=None):
if get_response is not None:
self.get_response = get_response
def __call__(self, request):
request.test_context = '1. __call__'
# print(request.test_context)
self.process_request(request)
response = self.get_response(request)
response = self.process_template_response(request, response)
return response
def process_request(self, request):
request.test_context += ' - 2. process_request'
# print(request.test_context)
def process_template_response(self, request, response):
request.test_context += ' - 4. process_template_response'
# print(request.test_context)
return response
def test_context(request):
request.test_context += ' - 3. view'
# print(request.test_context)
return render(request, 'my_app/test_context.html')
{{request.test_context}}
Answer the question
In order to leave comments, you need to log in
So far, I have solved it by a workaround:
from django.shortcuts import render
def render_test_context(request, template_name):
request.test_context += ' - 4. process_template_response'
# print(request.test_context)
return render(request, template_name)
from my_app.my_render import render_test_context
def test_context(request):
request.test_context += ' - 3. view'
# print(request.test_context)
return render_test_context(request, 'my_app/test_context.html')
If you are not satisfied with the result of generating a page from a template in a view, remake the template. Redo the view. For this, templates and views are invented.
The view renders a template based on the data. The rendered template (after the view) no longer has any {{request.test_context}} and other machinery. There is only ready to issue html. Therefore, no "- 4. process_template_response" will appear there.
Either use the tools for their intended purpose or do the post-process with your hands (change the generated html with greps and regexps). But this is a really strange decision. What could be missing BEFORE the view call, but appeared AFTER and you can't use it in the template now because it's too late?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question