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

11,120 views
Skip to first unread message

Sithembewena Lloyd Dube

unread,
Jun 26, 2012, 5:48:31 AM6/26/12
to django...@googlegroups.com
Hi everyone,

I have a form on which I'm calling save() and if it is successful I would like to display 'your post has been saved' or similar in the template. How would one go about that in Django 1.4?

I am sifting through the docs and cannot seem to find a definitive answer.

Thanks in advance.

--
Regards,
Sithembewena Lloyd Dube

罗健忠

unread,
Jun 26, 2012, 6:35:19 AM6/26/12
to django...@googlegroups.com
you may use HttpResponseRedirect to redirect to another page to display the successful message

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

Jirka Vejrazka

unread,
Jun 26, 2012, 8:20:49 AM6/26/12
to django...@googlegroups.com
Hi,

have you checked the messaging framework in Django?

HTH

Jirka

Sithembewena Lloyd Dube

unread,
Jun 26, 2012, 8:35:10 AM6/26/12
to django...@googlegroups.com
@newkedison, thank you for the suggestion. I wish to stay on the page without doing any redirects.

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

I ended up doing the following (which worked):

In view:
success = default value here
    if form.is_valid():
    form.errors['success'] = 'Saved'
....
return self.render_to_response(request, template_name, {
            'tomatoes': tomatoes,
            'success': success,
        })

... and in template:

<p>{{ success }}</p>

Regards.

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

Sithembewena Lloyd Dube

unread,
Jun 26, 2012, 8:37:18 AM6/26/12
to django...@googlegroups.com
I did do:
#if form.save():
    success = form.errors['success']

Jirka Vejrazka

unread,
Jun 26, 2012, 8:46:04 AM6/26/12
to django...@googlegroups.com
>> @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

maumercado

unread,
Jun 26, 2012, 11:26:50 AM6/26/12
to django...@googlegroups.com
Go with @JirkaV suggestion, also, you can add some jquery effect so that the div appears then fades out on success or on error... its pretty simple and really great!

Kurtis Mullins

unread,
Jun 26, 2012, 11:31:17 AM6/26/12
to django...@googlegroups.com
We do it all over our site. I use class-based views but you can checkout my "MessageMixin". I have the code on this stackoverflow page:


It will show up wherever you send the user to next, as long as your template is coded to display the message.

Iyengar8

unread,
Aug 3, 2015, 12:17:59 PM8/3/15
to Django users
But the message gets displayed only after I reload the page, the page isn't updated.

Sammy

unread,
Aug 4, 2015, 9:49:47 AM8/4/15
to Django users
I'm pretty sure you have to use AJAX to display the message without reloading.
Reply all
Reply to author
Forward
0 new messages