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
Click Action > URL Redirect. Give your action an internal title (something that makes sense to you in the survey). Enter the link to the site in the URL field. Scroll to the Fields To Pass and select the question, hidden value, or URL Variables you would like to pass in the Question to Send dropdown.
Flask class has a redirect() function. When called, it returns a response object and redirects the user to another target location with specified status code. location parameter is the URL where response should be redirected. statuscode sent to browser's header, defaults to 302.
If you want your form to submit to a different route you can simply do <form action="{{ url_for('app. login') }}"> . If you just want to put a link to the page use the <a> tag. If you want to process the request and then redirect, just use the redirect function provided by flask.
redirect returns a 302 header to the browser, with its Location header as the URL for the index function. render_template returns a 200, with the index. html template returned as the content at that URL.
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'))
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