Tanvir Hossain Bhuiyan
Tanvir Hossain Bhuiyan

Reputation: 787

Sending a value in context to all template renderd in Django without sending in context in every view

Suppose I have a variable in my settings.py file. Now I want this value to be accessed in all the templates all over the project. But I don't want to send this value in the context-data in every view. I want to send other data in context just like normally. But this specific one to be accessed in all templates. Is there any such process in Django?

For a little clarification, I wanted to implement the idea of "Interceptor" which we use to manipulate "HTTP" ajax requests.

Upvotes: 3

Views: 75

Answers (1)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52143

You can define a template context processor as described in here:

context_processors.py

from django.conf import settings

def interceptor(request):
    return {'interceptor': settings.HTTP_AJAX_INTERCEPTOR}

settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'project.context_processors.interceptor',
)

base.html

<html>
 ...
 <body>
  <div>{{ interceptor }}</div>
  ...
 </body>
</html>

Please see documentation for more information.

Upvotes: 6

Related Questions