Vivek S
Vivek S

Reputation: 5540

how to pass variables to templates only for some applications

I have a django project, with a lot of applications. Now i need to add some variables to the context dictionary only for some applications.Does django provide any option to achieve this? I know decorator can be helpful.Is there any other way,like a middleware / context processor that runs automatically but only for specific applications.

Upvotes: 0

Views: 72

Answers (1)

Amir Ali Akbari
Amir Ali Akbari

Reputation: 6406

A context processor like this can do what you need:

from django.core.urlresolvers import resolve

def app_var(request):
    if resolve(request.path).app_name == 'YOUR_APP_NAME':
        return {'CUSTOM_VAR': 'VALUE'}
    return {}

to install the context processor, put the code in any file you like, and add a entry referencing (e.g. 'folder.file.app_var') it in the CONTEXT_PROCESSORS in your settings.py.

Upvotes: 1

Related Questions