Skip to content

Instantly share code, notes, and snippets.

@jterrace
Created February 14, 2012 03:42
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jterrace/1823320 to your computer and use it in GitHub Desktop.
Save jterrace/1823320 to your computer and use it in GitHub Desktop.
Automatically log in user after django-registration activation
from registration.signals import user_activated
from django.contrib.auth import login, authenticate
def login_on_activation(sender, user, request, **kwargs):
"""Logs in the user after activation"""
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)
# Registers the function with the django-registration user_activated signal
user_activated.connect(login_on_activation)
@hpineda
Copy link

hpineda commented Nov 14, 2013

Hi! Is this working for you? I copied your code but seems like it's never executed

@seddonym
Copy link

Works for me, though I did it using a decorator. Did you make sure the code is loaded? For example, you could put it in the models.py for an installed app.

from django.dispatch import receiver
from registration.signals import user_activated
from django.contrib.auth import login


@receiver(user_activated)
def login_on_activation(sender, user, request, **kwargs):
    """Logs in the user after activation"""
    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)

@fpruitt
Copy link

fpruitt commented May 27, 2014

Thanks for this. Succinct and easy to understand.

@robertlagrant
Copy link

Thanks for this; I also didn't know where to put the signal.

Why do you need to specify user.backend here?

@mazelx
Copy link

mazelx commented Jul 1, 2015

Thanks a lot ! Still works in Registration-Redux. Should definitively be integrated into standard registration-redux.

User received through signal does not contain any auth backend, that's why you need to specify it in that code.

@AbdAhmad
Copy link

AbdAhmad commented May 5, 2021

It says 'No module named 'registration'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment