如何使用django中的{{next}}变量重定向到具有GET参数的页面 [英] How to redirect to page which has GET parameters after login using the {{next}} variable in django
问题描述
我正在使用 allauth
在我的django网站上提供注册和登录。除了我遇到问题之外,其他一切似乎都正常工作,因为我在登录后将人员重定向到当前页面。
I am using allauth
to provide registration and login in my django site. Everything else seems to be working fine other than that I am having problems to redirect the person to the current page after login.
我有一个页面,我有一些面试问题一个典型的网址就像
I have a page where I have some interview questions and a typical url for it would be like
/questions/?company=google
此页面包含公司google的问题列表,但要查看该人需要登录的答案。答案显示在下拉框中。然而,当用户点击登录时,请求将发送到登录
页面,如下所示
This page contains a list of questions for the company google, but to view the answer the person needs to login. The answers are displayed in a dropdown box. However when the user clicks on login a request is sent to the login
page as follows
/login/?next=/questions/
而实际在那里的get参数在我的实际页面中,因为我的url中的&
不发送。我如何解决这个问题。这个人看起来不太好,他被重定向到他/她试图登录的不同页面。
And the get parameter which was actually there in my actual page is not sent because of the &
in my url. How can I solve this problem. It does not look nice that the person is redirected to a different page from where he/she tried to login.
我知道发送 / code>参数作为
GET
变量不是解决方案,但有一种方法可以发送重定向链接
作为模板中的 POST
变量。
I know sending the next
parameter as a GET
variable is not the solution, but is there a way I can send the redirect link
as a POST
variable from the template.
我尝试过另一件事,在我看来,显示问题列表。我设置会话变量,其中包含当前链接的url。如果用户点击登录,在我的登录视图中,我检查这个特定的会话变量。如果设置了,那么我将重定向到该页面。
I tried another thing, in my view that displays the questions list. I set session variables which contains the url of the current link . If a user clicks on login, in my login view I check for this particular session variable. If it is set then I redirect to that page.
但是,在登录视图中没有收到会话变量,我不知道,但我认为当用户进入登录视图时会重置会话。
However the session variable is not received in the login view, I am not sure but I think the session is reset when the user goes to the login view.
任何建议都不胜感激。
推荐答案
参数在这种情况下。你想发送一个变量,如 / questions /?company = google
,但是你提到?
, =
(其中)字符是特殊字符。嵌入URL时具有特殊的意义。如果您使用URL编码编码变量,则变为%2Fquestions %2F%3Fcompany%3Dgoogle
。如果将其分配给参数 next
,则URL将变为: / login /?next =%2Fquestions%2F%3Fcompany%3Dgoogle
。这应该在登录时重定向到正确的地方。
You should encode the URL-parameter in this case. You want to send a variable like /questions/?company=google
, but as you mentioned the ?
, =
(amongst others) characters are special ones. It has a special meaning when embedded in the URL. If you encode the variable with URL encoding, it becomes %2Fquestions%2F%3Fcompany%3Dgoogle
. If you assign that to the parameter next
, the URL becomes: /login/?next=%2Fquestions%2F%3Fcompany%3Dgoogle
. This should redirect to the correct place on login.
这篇关于如何使用django中的{{next}}变量重定向到具有GET参数的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!