Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tornado: pattern for Flask's flashed messages

In Flask, flashing messages on redirect is done using the flash function in the view + {% for message in get_flashed_messages() %} in the template. Tornado doesn't seem to have anything like that built in (which is fine with me, fwiw).

Only replacement I've seen so far looks like this (part of this gist):

class AuthLoginHandler(BaseHandler):
    def get(self):
        errormessage = self.get_argument("error", default="")
        self.render("login.html", errormessage = errormessage)

    def post(self):
        ...(code)...
        if not auth:
            error_msg = u"?error=" + tornado.escape.url_escape("Login incorrect")
            self.redirect(u"/auth/login/" + error_msg)

And then in the template:

 <span class="errormessage">{{errormessage}}</span>

Is there a cleaner pattern?

(I can see how one could do multiple messages with this pattern, and a couple ways of cleaning it up, but that's not what I'm asking.)

like image 289
scharfmn Avatar asked Mar 16 '23 21:03

scharfmn


1 Answers

Flask's flash by default uses cookies, so a direct translation would be to use self.set_secure_cookie("flash", message) to set a message and self.get_secure_cookie("flash"); self.clear_cookie("flash") to read it back.

like image 180
Ben Darnell Avatar answered Mar 29 '23 23:03

Ben Darnell