Introduction to Context Processors
In this page:
What a Context Processor Does
Instead of manually adding a variable to every view's context dict, a context processor injects it into every template's context automatically.
Example: What a Context Processor Does
This plain Python function returns a dict; once registered, every template gains access to {{ site_name }} without any view passing it explicitly.
def site_settings(request):
return {'site_name': 'CookiesCursor'}
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Registering a Context Processor
Custom context processors must be listed by their dotted path in settings.py's TEMPLATES configuration.
Warning: The function must be listed by its full import path (e.g. 'myapp.context_processors.site_settings').
Example: Registering a Context Processor
After registering it, {{ site_name }} becomes usable in any template rendered with render(), with no per-view work.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'myapp.context_processors.site_settings',
],
},
},
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Using the Built-in auth Context Processor
django.contrib.auth.context_processors.auth is enabled by default in new projects and makes {{ user }} available in every template.
Example: Using the Built-in auth Context Processor
{{ user }} is available here purely because of the built-in auth context processor -- no view code passed it in.
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}!</p>
{% else %}
<p>Please log in.</p>
{% endif %}
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Trying to add heavy database queries inside a context processor, which slows down every single page since it runs on every render.
- Forgetting to register a custom context processor inside the context_processors list in settings.py's TEMPLATES config.
- Not realizing built-in context processors like request or auth already provide {{ request }} and {{ user }} for free in every template.
- A context processor is a function that adds variables to the context of every template automatically.
- Built-in processors like django.contrib.auth.context_processors.auth make {{ user }} available everywhere.
- Custom context processors are registered in settings.py's TEMPLATES[OPTIONS][context_processors] list.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: