I'm trying to redirect to a URL in Flask. The target URL that I'm trying to redirect to has a variable like this /dashboard/<username>
which has a view as follows,
@app.route('/dashboard/<username>')
def dashboard(username):
return render_template('dashboard.html', username=username)
How do I redirect to this URL using the Flask's redirect()
& url_for()
functions. I have tried this,
return redirect(url_for("index"))
which works fine as index is an URL without any variable part (/index
) in my application. But, how do I do it for URLs which have variable paths?
Thanks
You will want to create your URL with url_for
by giving it the name of your URL, the keyword arg
, and value for your URL parameter in the following manner:
return redirect(url_for('dashboard', username='foo'))