Search code examples
djangodjango-viewsdjango-registration

Auto log-in and re-send email


I have a django-registration up and working. I would like to add two additional features to it and am having a bit of difficulty understanding the inner-workings of the log-in process.

1) When a user clicks the activation email, it makes the account active but does not log the user in, how would I make it so clicking the activation link both makes the account active and automatically logs in the user? This is currently what my activate function looks like --

def activate(self, request, activation_key):
    activated = RegistrationProfile.objects.activate_user(activation_key)
    if activated:
        signals.user_activated.send(sender=self.__class__,
                                    user=activated,
                                    request=request)
        login (request, activated) ### if I try this line, it throws an error 'User'        
                                   ### object has no attribute 'backend
    return activated

update: I was able to add a hack to get this working, using sessions. Surely it's not the ideal solution, but here is what I have --

def register(self, request, **kwargs):
    ...        
    new_user.save()
    request.session['username'] = username
    request.session['password'] = password
    return new_user

def activate(self, request, activation_key):
    username = request.session['username']
    password = request.session['password']
    activated = RegistrationProfile.objects.activate_user(activation_key)
    if activated:
        signals.user_activated.send(sender=self.__class__,
                                    user=activated,
                                    request=request)
        user = authenticate(username=username, password=password)
        login(request, user)
    return activated

2) I would like to add an option for a user to be able to click a button to receive another activation email (should he fail to receive the first). It seems the following is where the activation email is sent upon registration --

  signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)

How would I send another activation email given the user account has already been created?


Solution

  • 1).

    from django.contrib.auth import login
    from registration import signals
    
    def login_on_activation(user, request, **kwargs):
        user.backend='django.contrib.auth.backends.ModelBackend'
        login(request, user)
    
    signals.user_activated.connect(login_on_activation)
    

    2). registration.models.RegistrationProfile.send_activation_email method.