2

我正在使用此链接上的教程进行延迟注册,并尝试将其与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('/')

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

4

1 回答 1

0

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

# 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)
于 2011-02-10T05:17:41.830 回答