【问题标题】:How to redirect to an external domain in Flask?如何重定向到 Flask 中的外部域?
【发布时间】:2014-04-28 14:04:54
【问题描述】:

在我的烧瓶应用程序中完成一个操作后,我需要重定向回一个外部 URL。 代码是这样的

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 失败。

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

【问题讨论】:

    标签: python flask


    【解决方案1】:

    我认为您需要在"www.google.com" 上附加前缀http://https://。否则 Flask 会将其视为应用程序内的相对 url。 所以下面将是一个 404,因为它将指向“localhost:5000/www.google.com”

    @app.route('/test') 定义测试(): 返回重定向(“www.google.com”)

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

    @app.route('/test') 定义测试(): 返回重定向(“http://www.google.com”)

    【讨论】:

      【解决方案2】:

      确保在重定向传递之前在 URL 前面附加“http://”。

      s = form.next.data
      if s is not None:
          if s.find("http://") != 0 and s.find("https://") != 0:
              s = "http://" + s
          return redirect(s)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-09
        • 2021-07-23
        • 2012-12-08
        • 1970-01-01
        • 1970-01-01
        • 2016-03-24
        相关资源
        最近更新 更多