'Fetch Data from DB in base template Django Python
I am developing an application (simple online store) with Python Django I have created a base.html file in templates and then all other html files extends from this file but the issue is this, I need the base.html file to have access to Database, so that I can show all names of the categories of products in the header of the file
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css" />
<title>{% block title %}My amazing site{% endblock %}</title>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" media="screen" href="{% static "css/main.css" %}"/>
<link rel="stylesheet" type="text/css" media="screen" href="{% static "bootstrap/css/bootstrap.min-rtl.css" %}" >
<link rel="stylesheet" type="text/css" media="screen" href="{% static "bootstrap/css/bootstrap-responsive.min-rtl.css" %}" >
</head>
<body class="container-fluid" >
<div class="row-fluid" >
<div id="sidebar" class="span12">
{% block sidebar %}
{% endblock %}
</div>
</div>
<div class="row-fluid">
<div class="span8 offset2">
{% block slider %}
{% endblock %}
</div>
</div>
</body>
</html>
how should I create some variables that can be accessible through whole application and I don't have to fetch them from db for every single pages separately ?
thanks in advance for any help
Solution 1:[1]
You can either write a template context processor or a template tag.
Template context processor
settings.py
from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
"myapp.processor.foos",
)
myapp/processor.py
from myproject.myapp.models import Foo
def foos(request):
return {'foos': Foo.objects.all()}
and then you can use {{ foos }} in any template
providing you pass the context_instance in your render_to_response
return render_to_response('index.html', {}, context_instance=RequestContext(request))
Template tags
Solution 2:[2]
You want to use template context processors. I couldn't find a very clear description in the Django documentation, but template context processors are basically callables that are called with a request, and return a dictionary of context variables to be made available for all templates (not strictly all templates, but if you use django.shortcuts.render
, you're covered).
Defining your active template context processors is a bit awkward, because you cannot just add your processor to the list of default processors, and you usually want to keep the default ones as well, so you need to dump the entire list of the default processors plus your processor in your settings file:
In your settings:
TEMPLATE_CONTEXT_PROCESSORS = (
# the default processors:
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
# your processor that will populate the context with a list of categories:
"myapp.context_processors.categories",
)
In your myapp/context_processors.py
:
def categories(request):
return {
'categories': Category.objects.all()
}
Modify filenames and category retrieval as needed.
Solution 3:[3]
Bringing this one up to date.
TEMPLATE_CONTEXT_PROCESSORS
isn't available in the latest versions and is now within the TEMPLATES setting.
For example using the accepted answer:
project/settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Default processors
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
# Custom processor
'myapp.context_processors.foos',
],
},
},
]
myapp/context_processors.py
from .models import Foo
def foos(request):
return {'foos': Foo.objects.all()}
Then use {{ foos }}
in all templates.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | lanzz |
Solution 3 | James Bellaby |