Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 7 of 10 · Django Templates
Interview question

What are Django context processors and how do they work? Django context processors क्या हैं और कैसे काम करते हैं?

Answer

Context processors are functions that add variables to the context of EVERY template rendered with RequestContext (which render() uses by default), avoiding the need to manually pass common data in every view.

# settings.py - built-in context processors registered by default
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',   # adds 'user'
                'django.contrib.messages.context_processors.messages',  # adds 'messages'
            ],
        },
    },
]

# Because of auth's context processor, {{ user }} is available in
# EVERY template automatically - you never manually pass 'user' from views

# CUSTOM context processor - myapp/context_processors.py
def site_settings(request):
    return {
        'site_name': 'My Bookstore',
        'current_year': 2024,
    }

def cart_info(request):
    if request.user.is_authenticated:
        count = request.session.get('cart_count', 0)
    else:
        count = 0
    return {'cart_item_count': count}

# Registering the custom processor in settings.py
TEMPLATES = [
    {
        'OPTIONS': {
            'context_processors': [
                # ... built-in ones ...
                'myapp.context_processors.site_settings',
                'myapp.context_processors.cart_info',
            ],
        },
    },
]

# Now EVERY template can use these without views passing them explicitly
# base.html
<footer>{{ site_name }} © {{ current_year }}</footer>
<span>Cart: {{ cart_item_count }}</span>

# Note: context processors only run for templates rendered via
# render() or RequestContext - not for Template().render(Context()) directly

Context processors वो functions हैं जो RequestContext से render होने वाले हर template के context में variables add करते हैं, हर view में common data manually pass करने से बचाते हैं।

# settings.py - built-in context processors
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# auth context processor की वजह से {{ user }} हर template में उपलब्ध है

# Custom context processor - myapp/context_processors.py
def site_settings(request):
    return {
        'site_name': 'My Bookstore',
        'current_year': 2024,
    }

def cart_info(request):
    if request.user.is_authenticated:
        count = request.session.get('cart_count', 0)
    else:
        count = 0
    return {'cart_item_count': count}

# settings.py में register करना
TEMPLATES = [
    {
        'OPTIONS': {
            'context_processors': [
                'myapp.context_processors.site_settings',
                'myapp.context_processors.cart_info',
            ],
        },
    },
]

# base.html
<footer>{{ site_name }} © {{ current_year }}</footer>
<span>Cart: {{ cart_item_count }}</span>

Was this answer clear?