获取“django-registration”将您发送到您最初尝试访问的页面

发布于 2024-12-12 14:08:07 字数 429 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫相离 2024-12-19 14:08:07

如果您查看负责通过电子邮件激活帐户的视图( Registration.views.activate)您会看到它接受一个 success_url 参数,该参数是“要重定向到的 URL 模式的名称”成功激活后。”

因此,您只需覆盖调用该视图的 url 并提供您希望重定向到的页面。

因此,在您自己的 urls.py 中:

from registration.views import activate
urlpatterns = patterns('',
    url(r'^activate/(?P<activation_key>\w+)/

或者,您可以在您自己的视图中包装 django-registrations activate 视图,并接受 GET 参数以重定向到:

from registration.view import activate
def custom_activate(request, backend,
         template_name='registration/activate.html',
         success_url=None, extra_context=None, **kwargs):
    success_url = request.GET.get('next', None)
    return activate(request, template_name=template_name, success_url=success_url, extra_context=None, **kwargs)

现在,在您的模板 registration/activation_email.txt 中。 html 您可以将重定向位置附加到链接:

{% url 'registration.view.activate' activation_key as a_url %}

Thanks! ....

{% autoescape off %}
<a href="http://{{ site.domain }}{{ a_url }}?next='http://somepage_or_url'">
    http://{{ site.domain }}{{ url_registration_activate }}/
</a>
{% endautoescape %}

Thanks!

编辑

好的,上面处理的是硬编码重定向。我猜这就是您想要的流程:

  1. 用户尝试访问某个页面
  2. 用户被重定向到登录/注册页面
  3. 用户在该页面上注册并收到一封电子邮件
  4. 用户激活电子邮件并被重定向到他们尝试的原始页面查看

这更加困难,因为他们在第一步中尝试查看的页面需要一直传递到第四步,而且我们知道,HTTP 是无状态的。

我想到的第一个建议是在注册时将重定向保存在会话变量中,然后在激活时检索它。为此,我们可以覆盖 django-registrations 默认后端 (它只是一个类,其中的方法概述了注册过程的功能并从视图中调用),特别是 register 和 post_activation_redirect 方法:

custom_backend.py

from registration.backends.default import DefaultBackend
class RedirectBackend(DefaultBackend):
    def register(self, request, **kwargs):
        request.session['redirect'] = request.GET.get("next",None)
        super(RedirectBackend, self).register(request, **kwargs)

    def post_activation_redirect(self, request, user):
        return(request.session['redirect'], (), {})

并确保 django-registration 实际上使用此后端,我们通过 urls.py 将其提供给视图:

url(r'^activate/(?P<activation_key>\w+)/
,
            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 您可以将重定向位置附加到链接:


编辑

好的,上面处理的是硬编码重定向。我猜这就是您想要的流程:

  1. 用户尝试访问某个页面
  2. 用户被重定向到登录/注册页面
  3. 用户在该页面上注册并收到一封电子邮件
  4. 用户激活电子邮件并被重定向到他们尝试的原始页面查看

这更加困难,因为他们在第一步中尝试查看的页面需要一直传递到第四步,而且我们知道,HTTP 是无状态的。

我想到的第一个建议是在注册时将重定向保存在会话变量中,然后在激活时检索它。为此,我们可以覆盖 django-registrations 默认后端 (它只是一个类,其中的方法概述了注册过程的功能并从视图中调用),特别是 register 和 post_activation_redirect 方法:

custom_backend.py


并确保 django-registration 实际上使用此后端,我们通过 urls.py 将其提供给视图:


,
    activate,
    {'backend': 'custombackend.RedirectBackend'},
    name='registration_activate'),
url(r'^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 您可以将重定向位置附加到链接:

编辑

好的,上面处理的是硬编码重定向。我猜这就是您想要的流程:

  1. 用户尝试访问某个页面
  2. 用户被重定向到登录/注册页面
  3. 用户在该页面上注册并收到一封电子邮件
  4. 用户激活电子邮件并被重定向到他们尝试的原始页面查看

这更加困难,因为他们在第一步中尝试查看的页面需要一直传递到第四步,而且我们知道,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 您可以将重定向位置附加到链接:

编辑

好的,上面处理的是硬编码重定向。我猜这就是您想要的流程:

  1. 用户尝试访问某个页面
  2. 用户被重定向到登录/注册页面
  3. 用户在该页面上注册并收到一封电子邮件
  4. 用户激活电子邮件并被重定向到他们尝试的原始页面查看

这更加困难,因为他们在第一步中尝试查看的页面需要一直传递到第四步,而且我们知道,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:

from registration.views import activate
urlpatterns = patterns('',
    url(r'^activate/(?P<activation_key>\w+)/

Alternatively you could wrap up django-registrations activate view in your own view and accept a GET parameter to redirect to:

from registration.view import activate
def custom_activate(request, backend,
         template_name='registration/activate.html',
         success_url=None, extra_context=None, **kwargs):
    success_url = request.GET.get('next', None)
    return activate(request, template_name=template_name, success_url=success_url, extra_context=None, **kwargs)

Now, in your template registration/activation_email.html you can append the redirection location to the link:

{% url 'registration.view.activate' activation_key as a_url %}

Thanks! ....

{% autoescape off %}
<a href="http://{{ site.domain }}{{ a_url }}?next='http://somepage_or_url'">
    http://{{ site.domain }}{{ url_registration_activate }}/
</a>
{% endautoescape %}

Thanks!

EDIT

Ok, so the above deals with hard coded redirects. I'm guessing this is the flow you want:

  1. User tries to go to a page
  2. User gets redirected to a login/registration page
  3. User signs up on that page and gets sent an email
  4. User activates email and gets redirected to the original page they tried to view

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

from registration.backends.default import DefaultBackend
class RedirectBackend(DefaultBackend):
    def register(self, request, **kwargs):
        request.session['redirect'] = request.GET.get("next",None)
        super(RedirectBackend, self).register(request, **kwargs)

    def post_activation_redirect(self, request, user):
        return(request.session['redirect'], (), {})

and to make sure django-registration actually uses this backend, we provide it to the views via our urls.py:

url(r'^activate/(?P<activation_key>\w+)/
,
            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:

  1. User tries to go to a page
  2. User gets redirected to a login/registration page
  3. User signs up on that page and gets sent an email
  4. User activates email and gets redirected to the original page they tried to view

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:


,
    activate,
    {'backend': 'custombackend.RedirectBackend'},
    name='registration_activate'),
url(r'^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:

  1. User tries to go to a page
  2. User gets redirected to a login/registration page
  3. User signs up on that page and gets sent an email
  4. User activates email and gets redirected to the original page they tried to view

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:

  1. User tries to go to a page
  2. User gets redirected to a login/registration page
  3. User signs up on that page and gets sent an email
  4. User activates email and gets redirected to the original page they tried to view

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:

梦开始←不甜 2024-12-19 14:08:07

您应该使用相同的装饰器 @login_required,django-registration 也使用它。

You should use the same decorator @login_required, django-registration uses that too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文