pass a response.flash variable with redirect?

406 views
Skip to first unread message

Michael Wills

unread,
Mar 6, 2008, 9:16:35 PM3/6/08
to web2py Web Framework
I was just wondering if there is already a default mechanism to pass a message for response.flash when redirecting. Using the cookbook app as an example, the show() function redirects to recipes() if there is no matching recipe id. How do we tell the user "there was no match" while redirecting?

limodou

unread,
Mar 6, 2008, 9:28:02 PM3/6/08
to web...@googlegroups.com
I think redirect will cause the browser reget the new url, so what you
want will corss two request, and maybe you can use session to do that?


--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou

DenesL

unread,
Mar 6, 2008, 9:56:14 PM3/6/08
to web2py Web Framework
How about:
redirect(URL(r=request,f='recipes',vars=dict(match='n')))

and then in def recipes() add:
if request.vars.match=='n':
response.flash='no match found'

mdipierro

unread,
Mar 6, 2008, 10:29:15 PM3/6/08
to web2py Web Framework
no no no no. You do.

session.flash="no match found" #instead of response.flash
redirect(URL(r=request,f='recipes'))

it is in the manual.

Michael Wills

unread,
Mar 6, 2008, 11:25:33 PM3/6/08
to web...@googlegroups.com
session.flash! Ah! I'll have to read the manual again. :-P I think my first readings left an impression in my mind that there was a way to do it, and I just totally forgot. Many thanks.

mdipierro

unread,
Mar 6, 2008, 11:27:13 PM3/6/08
to web2py Web Framework
mind that if you redirect and redirect again the message is lost
unless before the second redirection you to

session.flash=response.flash

to carry over the flash.

DenesL

unread,
Mar 7, 2008, 8:32:57 AM3/7/08
to web2py Web Framework
Tried the session.flash but it only works once, even in different
sessions.By that I mean:

goto recipes
select a category with no recipes
you get flash
go to recipes again
select categories that have recipes
then try an empty one again: no flash

open another browser window
go recipes
select empty category: no flash
???

Michael Wills

unread,
Mar 7, 2008, 8:43:52 AM3/7/08
to web...@googlegroups.com
Confirming this one as well. I tried with the show controller. Put in an id known to not exist, get redirected back to recipes() with the session flash but afterwards, no.

Michael Wills

unread,
Mar 7, 2008, 8:59:16 AM3/7/08
to web...@googlegroups.com
Man... I really have to test more thoroughly. My session.flash is working correctly. User error on my part. Working through some other forms stuff though.

DenesL

unread,
Mar 7, 2008, 9:16:05 AM3/7/08
to web2py Web Framework
What do you mean Michael?.
I added the code as posted by Massimo and tested with two different
computers and two different browsers.

BTW didn't the footnote on the web2py pages read something like
"2007,2008 web2by..." before?
Now it says "2007 Gluon ..."

Michael Wills

unread,
Mar 7, 2008, 9:29:24 AM3/7/08
to web...@googlegroups.com
To try out your case DenesL, I added

len(records)==0: session.flash='no recipes for this category'

With that, I have to select an empty category twice before session.flash shows. It's one step behind after that but it shows and it's consistent. That makes sense if session.flash is only meant to be used for redirections. In gluon.main.server_controller it sets the response.flash to session.flash and sets session.flash to None so that makes sense.

But you are seeing the flash the first time using session.flash?

DenesL

unread,
Mar 7, 2008, 10:11:26 AM3/7/08
to web2py Web Framework
I got the flash on both computers when the empty category was selected
the first time.
After that, no matter what I do, it won't flash: web2py cleanup,
browser close/purge/reopen, web2py stop/restart --> no flash.

Massimo Di Pierro

unread,
Mar 7, 2008, 10:40:18 AM3/7/08
to web...@googlegroups.com

when a pages is loaded if there is a session.flash it is copied into
response.flash (so that it is dispalyed) and then reset to None (so
that it is displayed only once).
This is the intended behavior.

Massimo

Massimo Di Pierro

unread,
Mar 7, 2008, 10:41:52 AM3/7/08
to web...@googlegroups.com
>
which pages?

DenesL

unread,
Mar 7, 2008, 10:48:35 AM3/7/08
to web2py Web Framework
All in the cookbook (app.source.130193843596.tar)

DenesL

unread,
Mar 7, 2008, 10:51:06 AM3/7/08
to web2py Web Framework
Even on a different session? or after web2py restart?

Massimo Di Pierro

unread,
Mar 7, 2008, 11:03:39 AM3/7/08
to web...@googlegroups.com
only once after the first redirection in the same session.

DenesL

unread,
Mar 7, 2008, 11:57:13 AM3/7/08
to web2py Web Framework
What I am saying is that you can not get the flash ever again.
Not in a new session, not even after you restart web2py.
Try it.

Massimo Di Pierro

unread,
Mar 7, 2008, 12:16:39 PM3/7/08
to web...@googlegroups.com
do not understand.

are you confirming the fact that a flash message is displayed only
once (as intended) or are you saying that session.flash does not work
for you?

Massimo

DenesL

unread,
Mar 7, 2008, 1:06:31 PM3/7/08
to web2py Web Framework
It is not working for me.

Massimo Di Pierro

unread,
Mar 7, 2008, 1:10:26 PM3/7/08
to web...@googlegroups.com
Try this:


def test1():
session.flash='oops'
redirect(URL(r=request,f='test2'))

def test2():
return dict()

when you visit test1, and you get redirected, don't you see oops?

Are you using your own layout.html? If so you have to make sure
somewhere you have

{{if response.flash}}{{=response.flash}}{{pass}}

Could this be the problem?

DenesL

unread,
Mar 7, 2008, 2:22:11 PM3/7/08
to web2py Web Framework
That works.

I added the if response.flash check in recipes.html and show.html but
still no flash.
Must be a logic problem in the recipe example. Did you try that one?.

BTW you are missing the colon in:
{{if response.flash:}}{{=response.flash}}{{pass}}
Reply all
Reply to author
Forward
0 new messages