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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With