将“django-registration”发送给您最初尝试访问的页面 [英] Getting `django-registration` to send you to the page you were originally trying to visit

查看:232
本文介绍了将“django-registration”发送给您最初尝试访问的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

django.contrib.auth 有一个很棒的功能:当您尝试访问由 login_required ,您将被重定向到具有下一个参数的登录页面,因此在您登录后,您将被重定向回您最初尝试访问的页面。



但是,显然 django-registration 不提供了类似的功能。我预计如果您注册而不是登录,您还将获得下一个事情,注册后,您将被重定向到您原来的页面试图去参观。不是这样,你只是重定向到一些成功的页面。这会伤害流程。



django-registration 可能提供此选项,但我没有使用它或正确?还是有一个简单的方法呢?

解决方案

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



所以你只需要覆盖调用该视图并提供要重定向到的页面的URL。



所以在您自己的 urls.py 中:

  from registration.views import activate 
urlpatterns = patterns('',
url(r'^ activate /(?
$ b {'backend':'registration.backends.default.DefaultBackend'},
name ='registration_activate',
#你可以使用reverse()而不是一个URL为DRY'er
success_url =http:// ...
),
/ pre>

或者,您可以在您自己的视图中包含django注册激活视图,并接受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.html 您可以将重定向位置附加到链接:

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

谢谢!

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

谢谢!

编辑



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


  1. 用户试图转到页面

  2. 用户被重定向到登录/注册页面

  3. 用户注册该页面并发送电子邮件

  4. 用户激活电子邮件并被重定向到他们尝试查看的原始页面

这是更困难的,因为他们尝试在第一步中查看的页面需要传递所有第四步的方式,我们知道,HTTP是无状态的。



首先要注意的是在注册时将重定向保存在会话变量中激活时检索它。为此,我们可以覆盖django注册默认后端(这只是一个类,其方法概括了注册过程的功能,并从视图中调用),特别是注册和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 (请求,** kwargs)

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

并确保django注册实际使用此后端,我们将其提供给通过我们的urls.py:

  url(r'^ activate /(?P< activation_key> \w +)/ $',
activate,
{'backend':'custombackend.RedirectBackend'},
name ='registration_activate'),
url(r'^ register / $'
注册,
{'后端':'custombackend.RedirectBackend'},
name ='registration_register'),


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?

解决方案

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+)/$',
            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:

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

这篇关于将“django-registration”发送给您最初尝试访问的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆