How to pass a message when we redirect to some template from django views?


I want to redirect from views in django but I want the redirect to go with some messages and that message should be displayed at redirected page.

asked 2 years, 3 months ago
Reputation: 1

1 Answer


We can use django messages framework for this.

Adding a message

To add a message, we need to use messages framework from django.

from django.contrib import messages

messages.add_message(request, messages.INFO, 'Hello world.')

Some shortcut methods provide a standard way to add messages with commonly used tags (which are usually represented as HTML classes for the message):

messages.debug(request, '%s SQL statements were executed.' % count), 'Three credits remain in your account.')
messages.success(request, 'Profile details updated.')
messages.warning(request, 'Your account expires in three days.')
messages.error(request, 'Document deleted.')

Displaying Messages

In your template add something like this

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>\r                                                                                       {% endif %}

That’s all! message will be sent and displayed on the redirected page.

answered 2 years, 3 months ago
Reputation: 1

Your Answer

Nothing to preview

Post Answer

Asked:  2 years, 3 months ago
Viewed:  4918 times
Active:  2 years, 3 months ago