Tuesday, June 26, 2012

Re: Django 1.4 - how to display a success message on form save

>> @Jirka - thanks. I saw something about the messaging framework and even
>> tried one example which did not work.

Using the messaging framework is actually very simple.

You need to enable the messaging framework (see the steps here:
https://docs.djangoproject.com/en/1.4/ref/contrib/messages/ )

In your template, you need this (I have that in my base template so
it's included in all pages):

{% if messages %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endif %}

Obviously, you'll need some formatting/CSS around it.

And in your views.py (or forms.py, ...)

from django.contrib import messages

if form.is_valid():
messages.success(request, 'Your form was saved')

And that's it!


Jirka

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment