django display message after POST form submit

The django admin uses django.contrib.messages, you use it like this:

In your view:

from django.contrib import messages

def my_view(request):
    ...
       if form.is_valid():
          ....
          messages.success(request, 'Form submission successful')

And in your templates:

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

Leave a Comment

tech