Django, global template variables
Asked Answered
C

6

32

I have a base template file (base.html) and every other template extends to it and generates content using its blocks. Certain variables, such as nav_obj, are used in the base template file.

View:

nav_obj = NavigationObject.objects.all()

Base template:

{% for object in nav_obj %}
<a href="{{ object.link }}">{{ object.title }}</a>
{% endfor %}

At the moment, I need to pass nav_obj in every view. Is there any way to have this sent automatically?

Crookes answered 8/2, 2010 at 17:15 Comment(1)
did you solved out ?Rowdy
F
34

Write your own context processor.

Four answered 8/2, 2010 at 17:19 Comment(3)
Thanks! Good details here : b-list.org/weblog/2006/jun/14/…Crookes
I've a question regarding global variables in Django views. Can please provide your views ? https://mcmap.net/q/102801/-django-app-level-variablesXavier
Can you please provide the code to do this?Ingratiating
C
9

Inclusion tags might be a good-looking alternative to a context processor.

Cinnabar answered 9/2, 2010 at 12:54 Comment(0)
R
8

As the accepted answers already says, use context processors. Here's how to make them work with the current Django version:

First, create a function which accepts a request and returns a dictionary with your global template variables:

def load_nav_obj(request):
    nav_obj = NavigationObject.objects.all()
    return {'nav_obj': nav_obj}

A good place for this function would be in a file context_processors.py in your main app.

Now, tell your app to use this context processor for all rendered templates. In your settings.py, add myapp.context_processors.load_nav_obj in the TEMPLATE settings:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your context processors here
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                ...
                'myapp.context_processors.load_nav_obj',
            ],
        },
    },
]

That's it! You can now use the variable {{nav_obj}} in all templates!

Replace answered 17/9, 2021 at 20:2 Comment(0)
E
4

There is an alternative, redirect here: Defining "global variable" in Django templates

Snippet example usage:

{% setglobal foo 0 %}
value={% getglobal foo %}
{% incrementglobal foo 0 %}
value={% setglobal foo %}
Endophyte answered 31/8, 2011 at 2:58 Comment(0)
F
0

You can also look at Django-navbar for it's documentation and tests..

Fluxmeter answered 8/2, 2010 at 17:43 Comment(0)
O
0

One easy workaround, I found is to create a simple Django template tag that will add the variables to the request object.

Example template tag:

# myapp/templatetags/custom_tags.py

from django import template
from.your_app.models import NavigationObject

register = template.Library()

@register.simple_tag(takes_context=True)
def nav_list(context):
    request = context['request']
    navigation_list = NavigationObject.objects.all()
    request.navigation_list = navigation_list
return ''

Then in your template:

{% load custom_tags %}
<!-- Add the custom template tag to set the global variable -->
{% nav_list %}
<!-- You can access the global variable as request.navigation_list -->
<h5>{{ request.navigation_list }}</h5>

This method will only work for templates that are loaded after the template tag is loaded, however, it has the added benefit of not needing to use a Context processor that will include the variable in every single template, which is very rarely needed.

Overtax answered 15/9, 2023 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.