G
G
GeorgeRu2019-08-06 10:15:51
Django
GeorgeRu, 2019-08-06 10:15:51

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


The question is how to update the content of request or response so that after self.get_response(request) the updated process_template_response data is displayed on the page?

Code snippet:

my_middleware.py
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


views.py
def test_context(request):
    request.test_context += ' - 3. view'
    # print(request.test_context)
    return render(request, 'my_app/test_context.html')


test_context.html
{{request.test_context}}

Using print I track the change in the request.test_context variable :
1. __call__
1. __call__ - 2. process_request
1. __call__ - 2. process_request - 3. view
1. __call__ - 2. process_request - 3. view - 4. process_template_response


But the html page displays:
1. __call__ - 2. process_request - 3. view

What needs to be fixed to see

1. __call__ - 2. process_request - 3. view - 4. process_template_response

instead

of 1. __call__ - 2. process_request - 3. view

?

Versions:
Django==2.1
Python==3.7

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GeorgeRu, 2019-08-08
@GeorgeRu

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')

A
Alexey Guest007, 2019-08-07
@Guest007

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 question

Ask a Question

731 491 924 answers to any question