← Back to Django Course | Chapter 7: Django Forms | Lesson 11 of 11

Django का Messages Framework

Messages framework Django का user के लिए एक छोटा sticky note छोड़ने का तरीका है, जैसे 'Saved!', जो एक बार show होता है और फिर गायब हो जाता है।
Syntax
markup
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.

markup
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.

markup
{% 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.

markup
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. #}
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. यह भूल जाना कि 'django.contrib.messages' INSTALLED_APPS में default से नहीं है इसकी removal, और MIDDLEWARE से MessageMiddleware missing होना, जिससे messages silently नहीं दिखते।
  2. Base template में {% for message in messages %} से loop न करना, इसलिए messages create तो होते हैं पर कभी render नहीं होते।
  3. 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 करना चाहिए।

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.