1π
β
To have some variable global to all templates, Django provide context processor
. A context processor is a Python function that takes the request object as an argument and returns a dictionary that gets added to the request context.
You can add the spaces
objects in the context processor like this :
app_name/context_processors.py
from .app_name import Space
def spaces(request):
# It must return a dictionary don't forget
return {'spaces': Space.objects.all()}
In your context processor, you instantiate the space using the request object and make it available for the templates as a variable named spaces
.
settings.py
And next, you need to add this custom context processor in the TEMPLATES
variable in settings.py
like this :
TEMPLATES = [
{
# ...
'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',
# Customs
'app_name.context_processors.space',
],
},
]
template.html
Now you have access of spaces
variable on all templates of the project.
<div class="sidenav">
<h2>Spaces:</h2>
<!-- spaces variable is in all templates -->
{% for space in spaces %}
<a href="/{{space}}">{{space}}</a>
{% endfor %}
</div>
π€Rvector
Source:stackexchange.com