Context Processors का परिचय
In this page:
# 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.
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 किया जाना चाहिए।
उदाहरण: Registering a Context Processor
After registering it, {{ site_name }} becomes usable in any template rendered with render(), with no per-view work.
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.
{% 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. #}
- एक context processor के अंदर heavy database queries add करने की कोशिश करना, जो हर single page को धीमा कर देता है क्योंकि यह हर render पर चलता है।
- settings.py के TEMPLATES config में context_processors list के अंदर एक custom context processor register करना भूल जाना।
- यह न 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 किए जाते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: