← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 9 of 10

Introduction to Context Processors

Context processors are helpers that quietly add the same extra info, like the logged-in user, to every single page automatically.

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.

markup
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.

markup
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.

markup
{% 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. #}
Common Mistakes
  1. Trying to add heavy database queries inside a context processor, which slows down every single page since it runs on every render.
  2. Forgetting to register a custom context processor inside the context_processors list in settings.py's TEMPLATES config.
  3. Not realizing built-in context processors like request or auth already provide {{ request }} and {{ user }} for free in every template.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.