A Django Context Processor is a Python function that takes an HttpRequest object and returns a dictionary of variables to be automatically merged into the template context across all templates rendered in the project.
Imagine you want to display the user’s shopping cart item count in the navbar of every single page on your site. Passing cart_count from every single view would violate DRY (Don’t Repeat Yourself). A context processor runs automatically before template rendering and injects that variable globally.
- A python function is defined that accepts
request. - The function returns a dictionary
{'cart_count': 5}. - The string path to this function is added to
OPTIONS['context_processors']inside theTEMPLATESsetting. - When
render()is called in any view, Django runs the processor and adds the dictionary to the template context.
graph TD A[View renders Template] --> B(Context Processor 1: Auth) A --> C(Context Processor 2: Custom Cart) B -->|injects user| D(Final Template Context) C -->|injects cart_count| D D --> E[HTML Rendered]
A Context Processor is like a global sponsor for an event. The specific speakers (Views) bring their own specific topics (context variables), but the global sponsor (Context Processor) automatically hands out a swag bag (global variables) to every attendee (Template) regardless of which speaker they are watching.
# context_processors.py
def site_name(request):
return {'SITE_NAME': 'My Awesome Website'}
# settings.py
TEMPLATES = [
{
# ...
'OPTIONS': {
'context_processors': [
# ...
'myapp.context_processors.site_name',
],
},
},
]- Runs globally for every template rendered with
RequestContext(whichrender()uses). - Useful for navbars, footers, and global settings.
- Can impact performance if it queries the database heavily on every page load.
- Built from: Django Template Engine — ties into the rendering pipeline.
- Related: Django View — augments the data provided by the view.
- Since context processors run on every template render, putting a slow database query inside one will globally degrade the performance of the entire application.