How to set flash message in Django 1.3 and show into another page


Set flash message like notification, error or something information and show into another page is quite easy in Django 1.3. If you don’t use render() or RequestContenxt, then you should enable message Middleware in settings.py :

1
2
3
4
5
6
7
8
9
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.request",
)

Set flash message into your method in views.py. This is for example :

1
2
3
4
5
6
from django.shortcuts import render, redirect
from django.contrib import messages

def index(request):
    messages.add_message(request, messages.INFO, ‘Example FLASH message here.’)              
    return render(request, ‘myapp/index.html’)

To show the messages in templates :

1
2
3
4
5
{% if messages %}
{% for message in messages %}
    {{ message }}
{% endfor %}
{% endif %}

Now, you can set flash message in any pages and send into another pages.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.