如何在Flask中重定向到外部域? [英] How to redirect to an external domain in Flask?

查看:59
本文介绍了如何在Flask中重定向到外部域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的flask应用程序中完成操作后,我需要重定向回外部URL.代码看起来像这样

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)

其中 form.next.data 可以是外部域(如"www.google.com")的绝对网址.但是,在将下一个值作为外部url传递时,此重定向将重定向到 http://mysitename/www.google.com 并失败,并显示404.

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.

如何指定重定向到外部域并阻止Flask将其附加到我的域根目录?

How do I specify that the redirect is to an external domain and stop Flask from appending it to my domain root?

推荐答案

我认为您需要在"www.google.com" .否则Flask会将其视为应用内的相对网址.因此下面将是一个404,因为它将转到"localhost:5000/www.google.com"

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")

但是,如果您尝试使用 http://,它应该可以正常工作.

But if you try with http://, it should work fine.


@app.route('/test')
def test():
    return redirect("http://www.google.com")

这篇关于如何在Flask中重定向到外部域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆