I need to redirect back to an external url after completing an action in my flask application. The code looks like this
if form.next.data is not None:
return redirect(form.next.data)
where form.next.data
can be an absolute url for an external domain like "www.google.com". However on passing the next value as an external url, this redirect is instead redirecting to http://mysitename/www.google.com
and failing with a 404.
How do I specify that the redirect is to an external domain and stop Flask from appending it to my domain root?
render_template FlaskGo to the project's root directory and create a file called run.py and enter the given code snippet in that file. Note that we have changed the port to 8080. If required, you can change the port in run.py. Now the development server will run on all network interfaces as we have specified 0.0.
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.
Flask redirect is defined as a function or utility in Flask which allows developers to redirect users to a specified URL and assign a specified status code. When this function is called, a response object is returned, and the redirection happens to the target location with the status code.
I think you need to append a prefix http://
or https://
to "www.google.com"
. Otherwise Flask is treating that as a relative url inside app.
So below will be a 404 as it's going to "localhost:5000/www.google.com"
@app.route('/test') def test(): return redirect("www.google.com")
But if you try with http://
, it should work fine.
@app.route('/test') def test(): return redirect("http://www.google.com")
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