Django का Messages Framework
from django.contrib import messages
messages.success(request, 'message') # also: info, warning, error
{% for message in messages %}
<p>{{ message }}</p>
{% endfor %}
एक Success Message जोड़ना
Redirect करने से तुरंत पहले एक view में messages.success() call करें, और यह एक बार next page पर दिखेगा जो user देखता है।
उदाहरण: Adding a Success Message
Call messages.success() in a view right before redirecting, and it will appear once on the next page the user sees.
from django.contrib import messages
from django.shortcuts import redirect
def save_article(request):
# ... form saved above ...
messages.success(request, 'Article saved successfully.')
return redirect('article-list')
{# 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. #}
एक Template में Messages Display करना
messages context variable, जिसे Django एक context processor के through अपने आप जोड़ता है, के ऊपर loop करें हर queued message render करने के लिए।
उदाहरण: Displaying Messages in a Template
Loop over the messages context variable, which Django adds automatically via a context processor, to render each queued message.
{% for message in messages %}
<div class="alert">{{ message }}</div>
{% endfor %}
{# 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. #}
Message Levels
messages.success, messages.error, messages.warning, और messages.info हर message को tag करते हैं ताकि templates उन्हें अलग तरीके से style कर सकें।
उदाहरण: Message Levels
messages.success, messages.error, messages.warning, and messages.info tag each message so templates can style them differently.
messages.error(request, 'Something went wrong while saving.')
{# 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. #}
- यह भूल जाना कि
'django.contrib.messages'INSTALLED_APPSमें default से नहीं है इसकी removal, औरMIDDLEWAREसेMessageMiddlewaremissing होना, जिससे messages silently नहीं दिखते। - Base template में
{% for message in messages %}से loop न करना, इसलिए messages create तो होते हैं पर कभी render नहीं होते। - Redirect करने से पहले
messages.success()call करना पर यह उम्मीद करना कि यह current page पर दिखेगा next page के बजाय — messages सिर्फ next request के across persist करते हैं।
django.contrib.messagesएक view को next page render के लिए एक one-time notification (success, error, info, warning) queue करने देता है।- Messages session या cookies में store होते हैं और एक बार display होने के बाद अपने आप clear हो जाते हैं।
- Base template को उन्हें असल में show करने के लिए
{{ messages }}context variable के ऊपर loop करना चाहिए।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: