javascript - 将 window.location 传递给 Flask url_for

标签 javascript python url flask

我正在使用 python。在我的页面上,当匿名用户进入登录页面时,我想将一个变量传递给后端,以便它指示用户来自何处(发送 URL)。

所以当用户点击这个 anchor 链接时:

<a href="{{ url_for('account.signin') }}">Sign in</a>

我想发送用户当前所在页面的当前 URL。

<script>
 var current_url = window.location.href;
<script>

我想这样发送:

<a href="{{ url_for('account.signin', current_url="window.location.href") }}">Sign in</a>

但是我不能在 url_for 中使用 javascript 代码,或者我该如何传递它?

最佳答案

使用request.path在渲染模板时获取路径。

<a href="{{ url_for('account.signin') }}?next={{ request.path }}">Sign in</a>

关于javascript - 将 window.location 传递给 Flask url_for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37862640/

相关文章:

linux - 将特定传出 url 重新路由到另一个 url - linux ubuntu 16.04

javascript - Google Contact API v3 通过 JavaScript 未创建联系人

javascript - 如何从div列表中获取当前数字

python - 如何使用 Tkinter 设置网格的位置?

python - 如何在单元测试中重用单元测试方法而不实际测试它

html - URL 中的 `#:~:text=` 如何突出显示文本?

javascript - 使用 javascript 复制粘贴

javascript - 打开标签,等待它完成加载然后关闭它

python - 尝试理解 model.predict 的输出

JavaScript 分割 URL 时无法获取片段