Reputation: 9826
I'm using Django 2.0
I have a Model within my notes
application
notes/models.py
class ColorLabels(models.Model):
title = models.CharField(max_lenght=50)
In the sidebar navigation which will be displayed on all pages, I want to show the list of color labels and it will show on all pages.
How can I show dynamic data in all views or say on all pages?
Upvotes: 1
Views: 327
Reputation: 9116
Use a custom template context processor.
This will add the context (data) you need into the templates rendered by all views.
First create the context processor (notes/context_processors.py
):
from .models import ColorLabel
def color_labels(request):
return {'color_labels': ColorLabel.objects.all()}
Then add it to the context_processors
option of your template renderer in settings.py
:
TEMPLATES = [{
'BACKEND': '...',
'OPTIONS': {
'context_processors': [
'notes.context_processors.color_labels',
...
],
},
}]
Finally, you will be able to use it in your templates:
templates/base.html
<nav>
{% for color_label in color_labels %}
{{ color_label.title }}
{% endfor %}
</nav>
Upvotes: 2