Login

login on activation with django-registration

Author:
morgan
Posted:
March 15, 2010
Language:
Python
Version:
1.1
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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 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.