Questions

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

0

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



asked 1 year, 10 months ago
Reputation: 1





1 Answer

0

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)
messages.info(request, '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 1 year, 10 months ago
Reputation: 1





Your Answer

Nothing to preview

Post Answer



Asked:  1 year, 10 months ago
Viewed:  3742 times
Active:  1 year, 10 months ago