user3827510
user3827510

Reputation: 149

Pass a value from a decorator to a context processor in django

In a context processors, I want to to be able to access a value that was set in a decorator. So the order of events would be:

The ultimate goal is to be able to conditionally run a context processor only for some views. Maybe there's a better way than using a decorator?

Upvotes: 1

Views: 559

Answers (1)

Daniel Robinson
Daniel Robinson

Reputation: 3387

You can have the decorator add a property to the request object, and then access that value in the context processor.

For example, you can use the following decorator:

def add_value(function):
    def wrap(request, *args, **kwargs):
        request.extra_value = True
        return function(request, *args, **kwargs)
    return wrap

Then you can access it in the context processor:

def extra_value_context_processor(request):
    if request.extra_value:
        ...

Upvotes: 2

Related Questions