Setting a variable in middleware to access a template

It seems to me hard to set a variable in one of my middleware classes, which I can access in the template.

The basic layout is as follows:

class TheMiddleware(object):
    def __init__(self, etc):
        stuff...

    def process_response(self, request, response):
        request.my_var = "whatever"
        return response

Then on the template for another view, I have:

{% custom_tag arg_a %}

What is a template tag that should return a variable from a request:

@register.simple_tag
def custom_tag(arg_a):
    return threading.currentThread().request.my_var

This is a bug with "Caught AttributeError when rendering: WSGIRequest object does not have attribute 'my_var'"

, , . django.core.context_processors.request TEMPLATE_CONTEXT_PROCESSORS, , , , .

, , . ? , , , .

, , , ?

( , !) , process_response , . , , . ( ).

, view1 view2. , . view2, , ​​, .

+5
2

.

, process_request() , .

def process_request(self, request):
    request.my_var = "whatever"
    return 
+7

, , threadlocals . , .

, process_response /: .. . process_request. . , .

+2

All Articles