Sometimes, we want to redirect while passing arguments with Python Flask.
In this article, we’ll look at how to redirect while passing arguments with Python Flask.
How to redirect while passing arguments with Python Flask?
To redirect while passing arguments with Python Flask, we can store values in a session.
For instance, we write
from flask import session, url_for
def do_baz():
messages = json.dumps({"main":"Condition failed on page baz"})
session['messages'] = messages
return redirect(url_for('.do_foo', messages=messages))
@app.route('/foo')
def do_foo():
messages = request.args['messages']
messages = session['messages']
return render_template("foo.html", messages=json.loads(messages))
to set session['messages']
to messages
in the do_baz
view with
session['messages'] = messages
and then do the redirect by calling redirect
.
Then in the do_foo
view, we get the value of
messages = session['messages']
Conclusion
To redirect while passing arguments with Python Flask, we can store values in a session.