我在用url_for
当用户注销时生成重定向 URL:
return redirect(url_for('.index', _external=True))
但是,当我将页面更改为https连接,将url_for
仍然给我http.
我想明确询问url_for
to add https在 URL 的开头。
你能指点我如何改变它吗?我查看了 Flask 文档,但没有运气。
使用 Flask 0.10,将有一个比包装更好的解决方案url_for
。如果你看https://github.com/mitsuhiko/flask/commit/b5069d07a24a3c3a54fb056aa6f4076a0e7088c7 https://github.com/mitsuhiko/flask/commit/b5069d07a24a3c3a54fb056aa6f4076a0e7088c7, a _scheme
参数已添加。这意味着您可以执行以下操作:
url_for('secure_thingy',
_external=True,
_scheme='https',
viewarg1=1, ...)
_scheme
设置 URL 方案,生成如下 URLhttps://..
代替http://
。但是,默认情况下,Flask 仅生成路径(没有主机或方案),因此您需要包含_external=True
去从/secure_thingy
to https://example.com/secure_thingy
.
但是,请考虑将您的网站设为仅 HTTPS。看来您只是尝试对少数“安全”路由部分强制执行 HTTPS,但如果链接到安全页面的页面未加密,则无法确保您的 https-URL 不会更改。这类似于混合内容 https://developers.google.com/web/fundamentals/security/prevent-mixed-content/what-is-mixed-content.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)