0

I created a model form.Is it possible to add some message in context in views.py ? Actually i want to display a success message in template page when form is submitted and data added in database.

for example i want to do this:

if form.save:
    msg = 'Data inserted successfully'
context = {'msg': msg,}

I want to save success message in my context so therefore i will show in my template page

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
habib
  • 63
  • 2
  • 15

1 Answers1

1

For showing the message after model save, you can follow this Stack Over Flow Question - Django: customizing the message after a successful form save

Please this messages functionality given by django framework for onetime messages. This is the second answer provided for the above question. There are a lot of ways mentioned in the documentation that could be implemented.

Simplest one is - In Views.py:

from django.contrib import messages
messages.add_message(request, messages.INFO, 'Data inserted successfully.')

In template:

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}
Rahul Reddy Vemireddy
  • 1,149
  • 1
  • 10
  • 14
  • this is not related to my question.it is about admin success message customization.I want to display message on template page – habib Aug 30 '17 at 06:36
  • @habib you should definitly re-read this answer and the docu it points to - the messages framework is not "about admin success messages", it's a general tool used to solve exactly the kind of problem you have. – bruno desthuilliers Aug 30 '17 at 06:51