On Feb 25, 2011, at 2:02 AM, SergeyPo wrote:
>
> Hello everyone!
>
> I want to do something like:
>
> def selector():
> try:
> x = db.headers[request.vars.ID]
> if x is None:
> response.flash = T('ID incorrect')
> redirect(URL(r=request, f='index', args=(request.args[1])))
> except:
> response.flash = T('ID incorrect')
> redirect(URL(r=request, f='headers', args=(request.args[1])))
>
> response.flash does not keep its value when you redirect. Is it
> possible to fix or workaround?
As others have already said, you want to use session.flash here. It's useful to
understand why.
Redirect is itself a response, sending a redirection code and the new URL to
the requesting browser. When that happens, everything in response, including
response.flash, is discarded--the redirection becomes the entire response.
On every request from the browser, web2py initializes a new response dictionary
(along with request and some other stuff). It sets response.flash =
session.flash, and then sets session.flash = None. So anything you store in
session.flash is used as response.flash for the *next* request/response, which
is exactly what you want when you're calling redirect.