RequestContext.
What are Context Processors?
Context processors are callables that take a request object and return a dictionary of items to be merged into the template context.Configuration
Configure context processors in your Django settings:Built-in Context Processors
django.template.context_processors
csrf
csrf
Provides CSRF token for form protection.Path: Implementation:Note: Always include this processor for security.
django.template.context_processors.csrfVariables added:csrf_token- CSRF token value or ‘NOTPROVIDED’
debug
debug
Provides debugging information when DEBUG=True.Path: Implementation:Configuration required:
django.template.context_processors.debugVariables added (only when DEBUG=True and IP in INTERNAL_IPS):debug- Boolean Truesql_queries- List of SQL queries executed
request
request
Adds the request object to the template context.Path: Implementation:Available request attributes:
django.template.context_processors.requestVariables added:request- The current HttpRequest object
request.method- GET, POST, etc.request.path- URL pathrequest.GET- GET parametersrequest.POST- POST datarequest.user- Current userrequest.session- Session data
i18n
i18n
Provides internationalization variables.Path: Implementation:
django.template.context_processors.i18nVariables added:LANGUAGES- List of available languagesLANGUAGE_CODE- Current language codeLANGUAGE_BIDI- Boolean, True if current language is bidirectional
tz
tz
Provides timezone information.Path: Implementation:
django.template.context_processors.tzVariables added:TIME_ZONE- Name of the current timezone
static
static
Adds STATIC_URL to the context.Path: Implementation:Note: Better to use
django.template.context_processors.staticVariables added:STATIC_URL- URL prefix for static files
{% load static %} and {% static %} tag.media
media
Adds MEDIA_URL to the context.Path: Implementation:
django.template.context_processors.mediaVariables added:MEDIA_URL- URL prefix for media files
csp
csp
Provides Content Security Policy nonce.Path: Implementation:
django.template.context_processors.cspVariables added:csp_nonce- Nonce value for CSP headers
django.contrib.auth.context_processors
auth
auth
Provides user and permissions information.Path: Permissions usage:
django.contrib.auth.context_processors.authVariables added:user- Current User instance (or AnonymousUser)perms- Permissions wrapper object
django.contrib.messages.context_processors
messages
messages
Provides flash messages.Path: Message levels:
django.contrib.messages.context_processors.messagesVariables added:messages- List of message objectsDEFAULT_MESSAGE_LEVELS- Message level constants
Creating Custom Context Processors
Basic Context Processor
Create a file (e.g.,myapp/context_processors.py):
Context Processor with Database Query
Context Processor with Settings
Conditional Context Processor
Performance-Optimized Context Processor
Best Practices
1. Keep Context Processors Lightweight
2. Use Lazy Objects for Expensive Operations
3. Cache Results When Appropriate
4. Avoid Name Collisions
5. Document Your Context Processors
Testing Context Processors
Common Patterns
Multi-tenancy
Feature Flags
Navigation
Debugging Context Processors
View all context variables in a template:{% debug %} tag: