Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i pass data to django layouts (like 'base.html') without having to provide it through every view?

I am trying to pass the data to layout 'base.html'. I am currently doing it by storing the data in request.session and accessing it in 'base.html' through request object.

Is there any way to pass the data to 'base.html' without having to pass the data from every views?

like image 527
Sachin K Avatar asked Jan 20 '16 14:01

Sachin K


People also ask

How do you pass variables from Django view to a template?

And this is rather simple, because Django has built-in template modules that makes a transfer easy. Basically you just take the variable from views.py and enclose it within curly braces {{ }} in the template file.

Which option does Django templates accept?

DjangoTemplates engines accept the following OPTIONS : 'autoescape' : a boolean that controls whether HTML autoescaping is enabled. It defaults to True . Only set it to False if you're rendering non-HTML templates!

What {{ name }} means in a Django template?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

Which characters are illegal in template variable name in Django?

Variable names consist of any combination of alphanumeric characters and the underscore ( "_" ) but may not start with an underscore, and may not be a number.


2 Answers

Use a context processor, which is made exactly for that purpose. Create a file context_processors.py in one of your app directories, then in the file define a function that return a dictionary of variables to insert in every template context, something like this:

def add_variable_to_context(request):     return {         'testme': 'Hello world!'     } 

Enable your context processor in the settings (django>=1.8):

TEMPLATES = [     {         'BACKEND': 'django.template.backends.django.DjangoTemplates',         'DIRS': [root('templates'),],         'APP_DIRS': True,         'OPTIONS': {             'context_processors': [                 'django.template.context_processors.debug',                 'django.template.context_processors.request',                 'django.contrib.auth.context_processors.auth',                 'django.contrib.messages.context_processors.messages',                 'yourapp.context_processors.add_variable_to_context',             ],         },     }, ] 

Then in every template you can write

{{ testme }}

And it will render as

Hello world!

More info in the Django documentation

like image 147
Augusto Destrero Avatar answered Oct 11 '22 15:10

Augusto Destrero


If you need this data in (almost) every template, then it makes sense to use a context processor. From the django docs:

The context_processors option is a list of callables – called context processors – that take a request object as their argument and return a dictionary of items to be merged into the context.

Django docs on Writing your own context processors

like image 26
Joni Bekenstein Avatar answered Oct 11 '22 16:10

Joni Bekenstein