Skip to content
Advertisement

redirect while passing arguments

In flask, I can do this:

JavaScript

And if foo.html contains {{ messages['main'] }}, the page will show hello. But what if there’s a route that leads to foo:

JavaScript

In this case, the only way to get to foo.html, if I want that logic to happen anyway, is through a redirect:

JavaScript

So, how can I get that messages variable to be passed to the foo route, so that I don’t have to just rewrite the same logic code that that route computes before loading it up?

Advertisement

Answer

You could pass the messages as explicit URL parameter (appropriately encoded), or store the messages into session (cookie) variable before redirecting and then get the variable before rendering the template. For example:

JavaScript

(encoding the session variable might not be necessary, flask may be handling it for you, but can’t recall the details)

Or you could probably just use Flask Message Flashing if you just need to show simple messages.

Advertisement