May 10, 2010

Q: response.flash or session.flash?

It’s simple, just use response.flash in the normal cases [there’s no redirection]

def index():
    response.flash = T('yes yes')
    return dict()

If you’re going to redirect user from one page to another, then you’ve to use session.flash = ….. because it’s the easiest way to keep the message across multiple requests.

def index():
    session.flash = T('yes yes')
    redirect(URL(request.application, 'default', 'function2'))
    eturn dict()

As you see : using response.flash is not correct because the user is going to be redirected which means another request and another response.

In fact, when a page is loaded if there is a session.flash it is copied into response.flash (so that it is displayed) and then reset to None
why resetting session.flash to None ? so that message is displayed only once.

So what if I’m going to do redirect then redirect again? how could I keep the flash message so that it’s displayed after the last redirection?

If you redirect and redirect again the message is lost unless before the second redirection you to