Flash messages supported with redirects?

207 views
Skip to first unread message

Marcel Overdijk

unread,
Mar 12, 2013, 5:54:00 PM3/12/13
to web...@googlegroups.com
I'm using flashes successfully using 'simple' renderings like:

def index(self):
        self.session.add_flash('Some message')
        self.render_response('index.html')

but when I use it in combination with a redirect the flashes seem to get lost, e.g.

def save(self):
    # do stuff
    self.session.add_flash('Some message')
    self.redirect_to('some_template')

I'm using Jinja2 and are providing the flashes to the templates as follows:

    def render_response(self, _template, **context):
        ctx = {
            'flashes': self.session.get_flashes(),
            'session': self.session
        }
        ctx.update(context)
        rv = self.jinja2.render_template(_template, **ctx)
        self.response.write(rv)


Am I doing something wrong or are flashes not supported with redirects?


Cheers,
Marcel

Marcel Overdijk

unread,
Mar 14, 2013, 3:54:54 PM3/14/13
to web...@googlegroups.com
I guess the problem was on my site.
The code I mentioned was simplified.

The actual code I was using was:

    def save(self):
        form = ProductForm(self.request.POST)
        if form.validate():
            product = Product()
            # store values..
            product.put()
            self.session.add_flash('Product %s is created' % product.code)
            self.redirect_to('show-product', id=product.key.id())
        self.render_response('product/create.html', form=form)

I thought the self.redirect_to would 'skip' the self.render_response which was not the case.
I think it was rendering on the 'background' accessing the flash and thus making it empty.

When I change the code to the one below it works.

    def save(self):
        form = ProductForm(self.request.POST)
        if form.validate():
            product = Product()
            # store values..
            product.put()
            self.session.add_flash('Product %s is created' % product.code)
            self.redirect_to('show-product', id=product.key.id())
        else:
            self.render_response('product/create.html', form=form)
Reply all
Reply to author
Forward
0 new messages