How to pass (one-time) message to redirect_to page?

1 view
Skip to first unread message

jerry

unread,
Jul 10, 2008, 4:46:22 PM7/10/08
to pylons-discuss
Hi,

Say the change password page is at GET h.url_for(controller='account',
action='password') and the new password is received via POST. After
updating the password in db the user is directed back with
redirect_to(controller='account', action='password').

But now how to display a _one-time_ message on the redirected page
telling the user that her password has been changed successfully?

Thanks.

Sincerely,
Jerry

Mike Orr

unread,
Jul 10, 2008, 5:42:47 PM7/10/08
to pylons-...@googlegroups.com

With WebHelpers 0.6:

# myapp/lib/helpers.py
from webhelpers.pylons import Flash
flash = Flash()

# Update action
h.flash("Password changed.")
redirect_to(...)

# Site template
<% flash_messages = h.flash.pop_messages() %>
% if flash_messages:
<ul id="flash-messages">
% for msg in flash_messages:
<li>${msg}</li>
% endfor msg
</ul>
% endif flash_messages


# Stylesheet
ul#flash-messages {
color: red;
background-color: #ffffcc;
font-size: larger;
font-style: italic;
margin-left: 40px;
padding: 4px;
list-style-type: none;
}

--
Mike Orr <slugg...@gmail.com>

Jonathan Vanasco

unread,
Jul 10, 2008, 11:11:32 PM7/10/08
to pylons-discuss
There are 3 ways:

1- Use that new flash functionality. neat.
2- Do a session based message yourself. I often have messages stored
in sessions mapped to a certain url, and visiting that url will
display + clear out.
3- Have the redirect have an arg like ?success=1 or &action=success


As a rundown of the options.
1. I don't know how flash works. Mike will have to comment.
2. I prefer this, because I get a bit of control and can do custom
validation and actions by storing & retrieving info as i see fit.
3- This is honestly the most popular version - every large webapp,
social network, even sites like facebook google myspace all go for
that route. There's no sort of sessioning or validation, it just
displays a standard 'success!' message on display.

jerry

unread,
Jul 11, 2008, 3:13:01 AM7/11/08
to pylons-discuss
1. Thanks Mike, awesome!

2. "messages stored in sessions mapped to a certain url" -- I don't
get it.

3. Thanks Jonathan (and silly me for not knowing/being able to come up
with such a simple idea).

Sincerely,
Jerry
Reply all
Reply to author
Forward
0 new messages