I'm aware that there is a context variable called "next" which allows you to specify the URL to redirect to after a page, which is commonly used when you want to redirect a user to a login page, then redirect them back to the page they were at before the login page once they've logged in.
我知道有一个名为“next”的上下文变量,它允许您指定在页面后重定向到的URL,这通常在您希望将用户重定向到登录页面时使用,然后将它们重定向回到他们登录后,他们在登录页面之前的页面。
In my views I've resorted to manually setting it via the URL such as redirecting to /login/?next=/someurl, but this isn't a clean way. I've tried googling and such, but there is surprisingly little information about it online.
在我的观点中,我已经通过URL手动设置它,例如重定向到/ login /?next = / someurl,但这不是一个干净的方法。我已经尝试使用谷歌搜索等,但网上的信息却令人惊讶。
How exactly do you set the context variable "next"? My site has a form that anyone can see, but only logged in users can submit. Right now if the user isn't logged in, they will get redirected to the login page with the "?next=/someurl/" attached to it so they get sent back to the original form once they log in.
你究竟如何将上下文变量设置为“下一个”?我的网站有一个任何人都可以看到的表单,但只有登录用户才能提交。现在,如果用户未登录,他们将被重定向到登录页面,并附加“?next = / someurl /”,这样一旦他们登录就会被发送回原始表单。
However, from the login page there is a link to the sign up page for users who don't have an account, and I want to set "next" to be the original form page so that after they sign up they are redirected back to the original form. Any ideas or advice?
但是,从登录页面可以看到没有帐户的用户注册页面的链接,我想将“下一步”设置为原始表单页面,以便在注册后重定向到原始形式。任何想法或建议?
1 个解决方案
#1
1
It sounds like you want to not simply use next
for one redirect, but persist it across two redirects: Some form page -> login -> signup -> Get back to some form
听起来你不想简单地使用next进行一次重定向,而是将其持久化到两个重定向:一些表单页面 - >登录 - >注册 - >回到某种形式
For the login page by itself, Django provides some automatic help for this (if you use the built-in auth views). But the second hop to the signup page requires some custom code explained below.
对于登录页面本身,Django为此提供了一些自动帮助(如果您使用内置的auth视图)。但是注册页面的第二跳需要一些自定义代码,如下所述。
If you are using Django's built-in login
view, and have created your own template for it, then Django will pass the current value of next
to the template as a context variable called (appropriately) next
. You can use that in the link to your sign-up page like this:
如果您正在使用Django的内置登录视图,并为其创建了自己的模板,那么Django会将模板旁边的当前值作为上下文变量调用(适当)。您可以在注册页面的链接中使用它,如下所示:
<a href="{% url 'your_signup_page' %}{% if next %}?next={{ next }}{% endif %}">Sign me up!</a>
Consequently, in the view you create to handle your user signup page, the value of next
will be accessible as a GET param:
因此,在您创建的用于处理用户注册页面的视图中,next的值将作为GET参数访问:
def signup(request):
if request.method == 'GET':
next = request.GET.get('next', None)
if next:
# Add it as a hidden input in your signup form
# Or re-append it as a GET param
# Or stick it in the user's session, which is
# what this example does:
request.session['next'] = next
Finally, in the same signup view, when you respond to the POSTed signup form, retrieve the next
value in whichever way you chose to propogate it in the GET request, and redirect to it's value.
最后,在同一个注册视图中,当您响应POSTed注册表单时,以您选择在GET请求中传播它的方式检索下一个值,并重定向到它的值。
def signup(request):
....
# POST handling section
if signup_form.is_valid():
next = request.session.get('next', None)
if next:
# See caution note below!
return redirect(next)
Caution:
警告:
Be aware that you should check and sanitize the next
value before you redirect to it after processing the signup form, to prevent browser-side tampering. For example, it's common to validate that the URL belongs to your own domain (if that's appropriate) and/or that Django's resolve
function is able to successfully resolve it.
请注意,在处理注册表单之后,应在检查并清理下一个值之前重定向到该值,以防止浏览器端的篡改。例如,通常验证URL属于您自己的域(如果合适)和/或Django的resolve函数能够成功解析它。
#1
1
It sounds like you want to not simply use next
for one redirect, but persist it across two redirects: Some form page -> login -> signup -> Get back to some form
听起来你不想简单地使用next进行一次重定向,而是将其持久化到两个重定向:一些表单页面 - >登录 - >注册 - >回到某种形式
For the login page by itself, Django provides some automatic help for this (if you use the built-in auth views). But the second hop to the signup page requires some custom code explained below.
对于登录页面本身,Django为此提供了一些自动帮助(如果您使用内置的auth视图)。但是注册页面的第二跳需要一些自定义代码,如下所述。
If you are using Django's built-in login
view, and have created your own template for it, then Django will pass the current value of next
to the template as a context variable called (appropriately) next
. You can use that in the link to your sign-up page like this:
如果您正在使用Django的内置登录视图,并为其创建了自己的模板,那么Django会将模板旁边的当前值作为上下文变量调用(适当)。您可以在注册页面的链接中使用它,如下所示:
<a href="{% url 'your_signup_page' %}{% if next %}?next={{ next }}{% endif %}">Sign me up!</a>
Consequently, in the view you create to handle your user signup page, the value of next
will be accessible as a GET param:
因此,在您创建的用于处理用户注册页面的视图中,next的值将作为GET参数访问:
def signup(request):
if request.method == 'GET':
next = request.GET.get('next', None)
if next:
# Add it as a hidden input in your signup form
# Or re-append it as a GET param
# Or stick it in the user's session, which is
# what this example does:
request.session['next'] = next
Finally, in the same signup view, when you respond to the POSTed signup form, retrieve the next
value in whichever way you chose to propogate it in the GET request, and redirect to it's value.
最后,在同一个注册视图中,当您响应POSTed注册表单时,以您选择在GET请求中传播它的方式检索下一个值,并重定向到它的值。
def signup(request):
....
# POST handling section
if signup_form.is_valid():
next = request.session.get('next', None)
if next:
# See caution note below!
return redirect(next)
Caution:
警告:
Be aware that you should check and sanitize the next
value before you redirect to it after processing the signup form, to prevent browser-side tampering. For example, it's common to validate that the URL belongs to your own domain (if that's appropriate) and/or that Django's resolve
function is able to successfully resolve it.
请注意,在处理注册表单之后,应在检查并清理下一个值之前重定向到该值,以防止浏览器端的篡改。例如,通常验证URL属于您自己的域(如果合适)和/或Django的resolve函数能够成功解析它。