url_for with _external=True 在 heroku 上不会在 URL 上附加服务器名称

mel*_*mel 7 python heroku flask python-3.x

我在 Heroku 上部署了一个应用程序,但问题是当我的应用程序发送电子邮件时,它没有在 URL 中附加我的服务器的名称:

content = Content("text/html", verification_email.format(user["first_name"],
                                                             url_for("register.display_register_form",
                                                                     token=token.decode("utf-8"), external=True)))
Run Code Online (Sandbox Code Playgroud)

但是我在电子邮件中收到的链接是:

http:///register_account/DnsJpXw_QIcPYeDHEg_fipB2kRiJBUj2RI6I9cI4Yl4w6K9ohbZRMVqBInuV0aOsBT4Zqt69X8MfhNfnys4s-DAQmgu1OPBwmSQnzAELvdcCyiZtkJGSY8_dQ799FOewtBDkvqR1D8XHmvVxgaVqbwSjdEBnvFsBBHMQCic%3D/verify?external=True
Run Code Online (Sandbox Code Playgroud)

我对这个 URL 有问题:

  • 它不是 https,应该是,因为它是 heroku 上的主机
  • ///当服务器名称为空时,服务器名称才不会出现在 URL 中

我该怎么做才能获得正确的 URL https://my-server-name/register_account...

编辑

我尝试在我的 config.py 文件中设置以下变量:

SERVER_NAME = " http://my-server-58140.herokuapp.com "

它在我的路径中生成了错误,例如我无法访问任何 URL,之前可以访问以下 URL,但在定义我的SERVER_NAME时不再访问它:

http://my-server-58140.herokuapp.com/home
Run Code Online (Sandbox Code Playgroud)

编辑

我的烧瓶应用程序已配置:

SERVER_NAME = os.environ.get('SERVER_NAME')
DEBUG = True
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
Run Code Online (Sandbox Code Playgroud)

0.0.0.0:5000我的本地主机和:my-server-58140.herokuapp.com在我的生产服务器上将环境变量设置为

Bov*_*sme 5

要生成正确的 URL,需要设置这两个 Flask 配置值:

  • SERVER_NAME 到您网站的名称,
  • PREFERRED_URL_SCHEMEhttps

请参阅有关以及如何配置 Flask 应用程序文档url_for()

编辑:此外,参数 inurl_for应该是_external而不是external