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

Context Processors का परिचय

Context processors वे helpers हैं जो चुपचाप हर single page में वही extra info, जैसे logged-in user, अपने आप जोड़ते हैं।
Syntax
markup
# app_name/context_processors.py
def processor_name(request):
    return {'key': value}

# settings.py -> TEMPLATES -> OPTIONS -> context_processors
'app_name.context_processors.processor_name',

एक Context Processor क्या करता है

हर view के context dict में manually एक variable add करने के बजाय, एक context processor इसे हर template के context में अपने आप inject करता है।

उदाहरण: 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. #}

एक Context Processor Register करना

Custom context processors को settings.py के TEMPLATES configuration में उनके dotted path से list किया जाना चाहिए।

Warning: Function को इसके full import path (जैसे 'myapp.context_processors.site_settings') से list किया जाना चाहिए।

उदाहरण: 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. #}

Built-in auth Context Processor उपयोग करना

django.contrib.auth.context_processors.auth नए projects में default रूप से enabled है और हर template में {{ user }} को available बनाता है।

उदाहरण: 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. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक context processor के अंदर heavy database queries add करने की कोशिश करना, जो हर single page को धीमा कर देता है क्योंकि यह हर render पर चलता है।
  2. settings.py के TEMPLATES config में context_processors list के अंदर एक custom context processor register करना भूल जाना।
  3. यह न realize करना कि built-in context processors जैसे request या auth पहले से हर template में {{ request }} और {{ user }} free में provide करते हैं।
चैप्टर सारांश
  • एक context processor एक function है जो हर template के context में variables अपने आप add करता है।
  • django.contrib.auth.context_processors.auth जैसे Built-in processors {{ user }} को everywhere available बनाते हैं।
  • Custom context processors settings.py के TEMPLATES[OPTIONS][context_processors] list में register किए जाते हैं।

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.