Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to an external domain in Flask?

Tags:

python

flask

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?

like image 975
suryasankar Avatar asked Apr 28 '14 14:04

suryasankar


People also ask

How do I redirect a template in Flask?

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.

How do I make a button action redirect to another page in Flask?

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.

What is return redirect 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.


1 Answers

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")
like image 193
xbb Avatar answered Sep 25 '22 16:09

xbb