Skip to content

Instantly share code, notes, and snippets.

@jterrace
Created February 14, 2012 03:42
Show Gist options
  • Select an option

  • Save jterrace/1823320 to your computer and use it in GitHub Desktop.

Select an option

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

hpineda commented Nov 14, 2013

Copy link
Copy Markdown

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

@seddonym

Copy link
Copy Markdown

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

fpruitt commented May 27, 2014

Copy link
Copy Markdown

Thanks for this. Succinct and easy to understand.

@robertlagrant

Copy link
Copy Markdown

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

Why do you need to specify user.backend here?

@mazelx

mazelx commented Jul 1, 2015

Copy link
Copy Markdown

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

AbdAhmad commented May 5, 2021

Copy link
Copy Markdown

It says 'No module named 'registration'

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