获取“django-registration”将您发送到您最初尝试访问的页面
django.contrib.auth
有一个很棒的功能:当您尝试访问由 login_required
修饰的页面时,您会被重定向到带有 next< 的登录页面。 /code> 参数,因此登录后您会被重定向回您最初尝试访问的页面。这对用户流量有好处。
但是,显然django-registration
没有提供类似的功能。我预计,如果您注册而不是登录,您还会得到一个 next
内容,并且在注册-n'-激活后,您将被重定向到您最初尝试访问的页面。情况并非如此,您只是被重定向到某个成功页面。这会伤害流量。
django-registration 是否可能提供此选项,但我没有使用它或没有正确使用它?或者有一个简单的方法可以做到这一点?
django.contrib.auth
has an awesome feature: When you try to access a page that's decorated by login_required
, you get redirected to the login page with a next
argument, so after you login you get redirected back to the page you were originally trying to access. That's good for the user flow.
But, apparently django-registration
does not provide a similar feature. I expected that if you register instead of login, you would also get a next
thing, and after registering-n'-activating you'll get redirected to the page you were originally trying to visit. This is not the case, you're just redirected to some success page. This hurts the flow.
Does django-registration
perhaps provide this option but I'm not using it or correctly? Or is there an easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看负责通过电子邮件激活帐户的视图( Registration.views.activate)您会看到它接受一个 success_url 参数,该参数是“要重定向到的 URL 模式的名称”成功激活后。”
因此,您只需覆盖调用该视图的 url 并提供您希望重定向到的页面。
因此,在您自己的 urls.py 中:
或者,您可以在您自己的视图中包装 django-registrations activate 视图,并接受 GET 参数以重定向到:
现在,在您的模板 registration/activation_email.txt 中。 html 您可以将重定向位置附加到链接:
编辑
好的,上面处理的是硬编码重定向。我猜这就是您想要的流程:
这更加困难,因为他们在第一步中尝试查看的页面需要一直传递到第四步,而且我们知道,HTTP 是无状态的。
我想到的第一个建议是在注册时将重定向保存在会话变量中,然后在激活时检索它。为此,我们可以覆盖 django-registrations 默认后端 (它只是一个类,其中的方法概述了注册过程的功能并从视图中调用),特别是 register 和 post_activation_redirect 方法:
custom_backend.py
并确保 django-registration 实际上使用此后端,我们通过 urls.py 将其提供给视图:
, register, {'backend': 'custombackend.RedirectBackend'}, name='registration_register'), , activate, {'backend': 'registration.backends.default.DefaultBackend'}, name='registration_activate', # You could use reverse() here instead of a URL to be DRY'er success_url = "http://..." ),或者,您可以在您自己的视图中包装 django-registrations activate 视图,并接受 GET 参数以重定向到:
现在,在您的模板 registration/activation_email.txt 中。 html 您可以将重定向位置附加到链接:
编辑
好的,上面处理的是硬编码重定向。我猜这就是您想要的流程:
这更加困难,因为他们在第一步中尝试查看的页面需要一直传递到第四步,而且我们知道,HTTP 是无状态的。
我想到的第一个建议是在注册时将重定向保存在会话变量中,然后在激活时检索它。为此,我们可以覆盖 django-registrations 默认后端 (它只是一个类,其中的方法概述了注册过程的功能并从视图中调用),特别是 register 和 post_activation_redirect 方法:
custom_backend.py
并确保 django-registration 实际上使用此后端,我们通过 urls.py 将其提供给视图:
If you look at the view responsible for the activation of an account via email (registration.views.activate) you'll see that it accepts a success_url parameter which is "The name of a URL pattern to redirect to on successful activation."
So you simply have to overwrite the url that calls that view and provide the page you wish to redirect to.
So in your own urls.py:
Alternatively you could wrap up django-registrations activate view in your own view and accept a GET parameter to redirect to:
Now, in your template registration/activation_email.html you can append the redirection location to the link:
EDIT
Ok, so the above deals with hard coded redirects. I'm guessing this is the flow you want:
This is more difficult as the page they were trying to view in step one needs to be passed all the way to step four and as we know, HTTP is stateless.
The first suggestion that comes to mind is to save the redirect in a session variable when you register and then retrieve it when you activate. To do this we can overwrite django-registrations default backend (which is just a class with methods that outline the functionality of the registration process and are called from the views), specifically the register and post_activation_redirect methods:
custom_backend.py
and to make sure django-registration actually uses this backend, we provide it to the views via our urls.py:
, register, {'backend': 'custombackend.RedirectBackend'}, name='registration_register'), , activate, {'backend': 'registration.backends.default.DefaultBackend'}, name='registration_activate', # You could use reverse() here instead of a URL to be DRY'er success_url = "http://..." ),Alternatively you could wrap up django-registrations activate view in your own view and accept a GET parameter to redirect to:
Now, in your template registration/activation_email.html you can append the redirection location to the link:
EDIT
Ok, so the above deals with hard coded redirects. I'm guessing this is the flow you want:
This is more difficult as the page they were trying to view in step one needs to be passed all the way to step four and as we know, HTTP is stateless.
The first suggestion that comes to mind is to save the redirect in a session variable when you register and then retrieve it when you activate. To do this we can overwrite django-registrations default backend (which is just a class with methods that outline the functionality of the registration process and are called from the views), specifically the register and post_activation_redirect methods:
custom_backend.py
and to make sure django-registration actually uses this backend, we provide it to the views via our urls.py:
您应该使用相同的装饰器 @login_required,django-registration 也使用它。
You should use the same decorator @login_required, django-registration uses that too.