【问题标题】:Post-methods to integrate Lazy Registration with django-registration将 Lazy Registration 与 django-registration 集成的后期方法
【发布时间】:2011-01-31 03:24:14
【问题描述】:

我正在使用the tutorial at this link 进行延迟注册,我正在尝试将它与django-registration 结合使用。

惰性注册链接上的教程只需要调用一个命令来重新设置保存的事件:

def on_registration_complete(self, request):
    Wishlist.reparent_all_my_session_objects(request.session, request.user)
    return HttpResponseRedirect('/')

def on_login_complete(self, request, user, openid=None):
    Wishlist.reparent_all_my_session_objects(request.session, request.user)
    return HttpResponseRedirect('/')

如何在用户登录或注册后使用 django-registration 进行 post-hook 以调用 reparent_all_my_session_objects() 命令?我是否需要通过从 django.contrib.auth 复制来创建自己的 auth

【问题讨论】:

    标签: django django-registration lazy-registration


    【解决方案1】:

    需要在注册和登录时创建自己的信号接收器。

    # Handle the signal sent by user_login
    from registration.signals import user_login, user_registered
    from events.models import Event
    from django.contrib.auth import authenticate, login
    
    # Use the signal sent after the login wrapper
    def user_login_handler(sender, **kwargs):
        """signal intercept for user_login"""
        request = kwargs['request']
        Event.reparent_all_my_session_objects(request.session, request.user)
    
    def user_registered_handler(sender, **kwargs):
        """signal intercept for user_registered"""
        request = kwargs['request']
        # Authenticate user, so that a User model (instead of AnonymousUser) is assigned to Event
        # Registration form validates password1==password2
        new_user = authenticate(username=request.POST['username'], password=request.POST['password1'])
        login(request, new_user)
        Event.reparent_all_my_session_objects(request.session, new_user)
    
    user_login.connect(user_login_handler)
    user_registered.connect(user_registered_handler)
    

    【讨论】:

      猜你喜欢
      • 2013-05-21
      • 2011-02-01
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 2013-05-05
      相关资源
      最近更新 更多