views:

1961

answers:

7

I have a view that accepts a form submission and updates a model.

After updating the model, I want to redirect to another page and I want a message such as "Field X successfully updated" to appear on this page.

How can I "pass" this message to the other page? HttpResponseRedirect only accepts a url. I've seen this done before on other sites. How is this accomplished?

A: 

Can you just pass the message as a query param oon the URL to which you're redirecting? It's not terribly RESTy, but it ought to work:

return HttpResponseRedirect('/polls/%s/results/?message=Updated" % p.id)

and have that view check for a message param, scrub it for nasties, and display it at the top.

Ry4an
+5  A: 

This is a built-in feature of Django, called "messages"

See http://docs.djangoproject.com/en/dev/topics/auth/#messages

From the documentation:

A message is associated with a User. There's no concept of expiration or timestamps.

Messages are used by the Django admin after successful actions. For example, "The poll Foo was created successfully." is a message.

S.Lott
Doesn't this feature requires an authenticated user? Or are there anonymous/unauthenticated users supported by Django?
Ionuț G. Stan
Doesn't require an authenticated user. The 'user' object to which the message gets attached can be an anonymous user; django creates a user object based off the session started for that visitor. Only requirement is that your visitor allow cookies.
Jarret Hardie
@Jarret and @S.Lott are wrong. Django's built-in messages are not session-based, they are database-backed and tied into actual DB user objects (the "anonymous" user does not exist in the DB), so they are only usable with authenticated users. Try django-flashcookie or django-session-messages.
Carl Meyer
Doesn't work for anonymous users. Plus, it's been deprecated.
ionut bizau
The trunk version(future 1.2) of Django support sending messages to anonymous users
Pydev UA
+3  A: 

You can use django-flashcookie app http://bitbucket.org/offline/django-flashcookie/wiki/Home

it can send multiple messages and have unlimited types of messages. Lets say you want one message type for warning and one for error messages, you can write

def simple_action(request):
    ...
    request.flash['notice'] = 'Hello World'
    return HttpResponseRedirect("/")

or

def simple_action(request):
    ...
    request.flash['error'] = 'something wrong'
    return HttpResponseRedirect("/")

or

def simple_action(request):
    ...
    request.flash['notice'] = 'Hello World'
    request.flash['error'] = 'something wrong'
    return HttpResponseRedirect("/")

or even

def simple_action(request):
    ...
    request.flash['notice'] = 'Hello World'
    request.flash['notice'] = 'Hello World 2'
    request.flash['error'] = 'something wrong'
    request.flash['error'] = 'something wrong 2'
    return HttpResponseRedirect("/")

and then in you template show it with

{% for message in flash.notice %}
    {{ message }}
{% endfor }}

or

{% for message in flash.notice %}
    {{ message }}
{% endfor }}
{% for message in flash.error %}
    {{ message }}
{% endfor }}
+1  A: 

while all suggestions above work, I would suggest going with Ry4an's (pass it in the request URL) - just change the actual text to a coded text within a predefined set of text messages.

Two advantages here:

  1. Less chance of something hacking through your scrubbing of bad content
  2. You can localize your messages later if needed.

The other cookie related methods.. well, they don't work if the browser doesn't support cookies, and are slightly more expensive.. but only slightly. They're indeed cleaner to the eye.

Liorsion
A: 

Take a look at Django's messages framework. http://docs.djangoproject.com/en/dev/ref/contrib/messages/#ref-contrib-messages

ionut bizau
A: 

There is a lot of solutions

1 Use Django-trunk version - it support sending messages to Anonymous Users

2 Sessions

def view1(request):
    request.session['message'] = 'Hello view2!'
    return HttpResponseRedirect('/view2/')


def view2(request):
    return HttpResponse(request.session['message'])

3 redirect with param

return HttpResponseRedirect('/view2/?message=Hello+view2')

4 Cookies

Pydev UA
A: 

You could also have the redirect url be the path to an already parameterized view.

urls.py:

(r'^some/path/(?P<field_name>\w+)/$', direct_to_template,
    {'template': 'field_updated_message.html',
    },
    'url-name'
),

views.py:

HttpResponseRedirect( reverse('url-name', args=(myfieldname,)) )

Note that args= needs to take a tuple.