Sometimes, we want to pass arguments into redirect(url_for()) of Flask.
In this article, we’ll look at how to pass arguments into redirect(url_for()) of Flask.
How to pass arguments into redirect(url_for()) of Flask?
To pass arguments into redirect(url_for()) of Flask, we define the destination route to get the request parameters.
Then we can call url_for with the parameters.
For instance, we write
@app.route("/found/<email>/<list_of_objects>")
def found(email, list_of_objects):
return render_template("found.html", keys=email, obj=list_of_objects)
to add the /found/<email>/<list_of_objects> route that maps to the found function.
In it, we get the the URL parameters from the found function’s parameters.
Then in another route, we write
return redirect(url_for("found", email=x, list_of_objects=y))
to call url_for with the route name and the parameters to return the URL for the /found/<email>/<list_of_objects> route with the parameters filled in.
Then we call redirect with the URL to redirect to the /found/<email>/<list_of_objects> route.
Conclusion
To pass arguments into redirect(url_for()) of Flask, we define the destination route to get the request parameters.