Login

login on activation with django-registration

Author:
morgan
Posted:
March 15, 2010
Language:
Python
Version:
1.1
Tags:
login auth django-registration
Score:
7 (after 7 ratings)

Here's a signal handler to log a user in on registration activation. It took me an hour to figure out that I needed to put the user.backend in quotes and google wasn't being my friend.

from the django-registration documentation: How do I log a user in immediately after registration or activation? You can most likely do this simply by writing a function which listens for the appropriate signal; your function should set the backend attribute of the user to the correct authentication backend, and then call django.contrib.auth.login() to log the user in.

1
2
3
4
def login_on_activation(sender, user, request, **kwargs):
    user.backend='django.contrib.auth.backends.ModelBackend' 
    login(request,user)
user_activated.connect(login_on_activation)

More like this

  1. Log in a user without requiring credentials by SmileyChris 6 years, 2 months ago
  2. Show logged users - keeping track of users login and logout by albertorcf 2 years, 11 months ago
  3. easy signal registration by aisbaa 3 years, 10 months ago
  4. Registration with confirmation email by catellar 3 years, 11 months ago
  5. Binding signals to abstract models by andreterra 3 years, 2 months ago

Comments

ericcolon (on March 17, 2010):

I was just looking for this. Thanks.

#

robhudson (on March 23, 2010):

Just a note: This requires the unreleased version of django-registration. The version that's released on PyPI (and that you will get via pip), doesn't appear to emit signals.

#

rich00 (on December 10, 2010):

Need a little help? What is 'sender' in this function? My thoughts were to call this function from with the registration.activate view.

#

HM (on September 6, 2013):

In my particular setup right now, doing an authenticate (which sets backend) won't work until after a redirect, so it's also useful without django-registration!

#

Gauravwagh11 (on August 14, 2014):

Where shoul i put this code? And what url should i use?

#

Please login first before commenting.