将 window.location 传递给 Flask url_for [英] Pass window.location to Flask url_for

查看:18
本文介绍了将 window.location 传递给 Flask url_for的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using python.On my page when an anonymous user goes to the sign in page, I want to pass a variable to the backend so it indicates where the user is coming from ( send the URL).

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

So when the user clicks on this anchor link:

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

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

I want to send current URL of the page where the user is currently.

<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 代码,或者如何传递它?

But I can't use javascript code inside url_for or how can I pass it?

推荐答案

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

Use request.path to get the path while rendering the template.

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

这篇关于将 window.location 传递给 Flask url_for的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆