admin管理员组文章数量:1331612
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 ing from ( send the URL).
So when the user clicks on this anchor link:
<a href="{{ url_for('account.signin') }}">Sign in</a>
I want to send current URL of the page where the user is currently.
<script>
var current_url = window.location.href;
<script>
I thought to send it like this:
<a href="{{ url_for('account.signin', current_url="window.location.href") }}">Sign in</a>
But I can't use javascript code inside url_for or how can I pass it?
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 ing from ( send the URL).
So when the user clicks on this anchor link:
<a href="{{ url_for('account.signin') }}">Sign in</a>
I want to send current URL of the page where the user is currently.
<script>
var current_url = window.location.href;
<script>
I thought to send it like this:
<a href="{{ url_for('account.signin', current_url="window.location.href") }}">Sign in</a>
But I can't use javascript code inside url_for or how can I pass it?
Share Improve this question edited Jun 16, 2016 at 16:54 davidism 127k31 gold badges415 silver badges347 bronze badges asked Jun 16, 2016 at 14:52 BabelBabel 6192 gold badges10 silver badges23 bronze badges 1- 1 Are u using Flask or Django Framework? – Iron Fist Commented Jun 16, 2016 at 15:30
4 Answers
Reset to default 4Use request.path
to get the path while rendering the template.
<a href="{{ url_for('account.signin') }}?next={{ request.path }}">Sign in</a>
Assuming you're using Django.
In your view you can return the previous location that the User can from:
from django.http import HttpResponseRedirect
def foo(request, *a, **kw):
# sign in user
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Or with Jquery:
Add an ID to the link.
<a href="{{ url_for('account.signin') }}" id="signin">
Then add the next parameter and redirect the browser to the new url.
$("#signin").click(function(e){
e.preventDefault()
window.location = $(this).href + "?next=" + window.location.href;
}
should produce something like: url/for/signin?next=prev/location
In your view you can access that next variable like so:
def foo(request, *a, **kw):
next_url = request.GET["next"]
I think you should use the request
object at its best, since it does provide a headers
dictionary which has all request headers, quoting from flask's Docs:
headers
The ining request headers as a dictionary like object.
Now, one of the headers elements is the HTTP_Referer
, which is :
The HTTP referer (originally a misspelling of referrer1) is an HTTP header field that identifies the address of the webpage (i.e. the URI or IRI) that linked to the resource being requested. By checking the referrer, the new webpage can see where the request originated.
Finally, you can access it from flask
as you would access any dictionary item:
>>> print('Referer is {}'.format(request.headers.get('Referer')))
You can use request.path
to get the current URL path (you can also use request.full_path
which includes the query string part of the current URL path), but no need to construct the query string by yourself, just pass it to current_url
.
Any keyword parameters pass to url_for
that isn't a URL variable will bee a query parameter automatically:
<a href="{{ url_for('account.signin', current_url=request.path) }}">Sign in</a>
本文标签: javascriptPass windowlocation to Flask urlforStack Overflow
版权声明:本文标题:javascript - Pass window.location to Flask url_for - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742258078a2441996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论