gpt4 book ai didi

python - flask url_for 生成 http URL 而不是 https

转载 作者:IT老高 更新时间:2023-10-28 21:43:15 29 4
gpt4 key购买 nike

当用户退出时,我正在使用 url_for 生成重定向 URL:

return redirect(url_for('.index', _external=True))

但是,当我将页面更改为 https 连接时,url_for 仍然给我 http

我想明确要求 url_for 在 URL 的开头添加 https

你能告诉我如何改变它吗?我查看了 Flask 文档,没有运气。

最佳答案

使用 Flask 0.10,将会有比包装 url_for 更好的解决方案。如果你看 https://github.com/mitsuhiko/flask/commit/b5069d07a24a3c3a54fb056aa6f4076a0e7088c7 ,添加了一个 _scheme 参数。这意味着您可以执行以下操作:

url_for('secure_thingy',
_external=True,
_scheme='https',
viewarg1=1, ...)

_scheme 设置 URL 方案,生成类似 https://.. 的 URL,而不是 http://。但是,默认情况下,Flask 只生成路径(没有主机或方案),因此您需要包含 _external=True 以从 /secure_thingy 转到 https://example.com/secure_thingy.


但是,请考虑将您的网站改为仅使用 HTTPS。 您似乎正在尝试仅对少数“安全”路由部分强制执行 HTTPS,但您无法确保您的 https - 如果链接到安全页面的页面未加密,则不会更改 URL。这类似于 mixed content .

关于python - flask url_for 生成 http URL 而不是 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14810795/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com