ok 5 years latter but this works for me with django 1.8 and python 2.7
The context is: the user create a new account then the admin receives an email to verify the user and chage active to True when the admin makes the change the user receives an email telling that now he can log in.
Sorry for my bad english.
#forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
#A register form that save field is_active as False
class RegisterForm(UserCreationForm):
email = forms.EmailField(label=_("E-mail"))
first_name = forms.CharField(label=_("First Name"))
last_name = forms.CharField(label=_("Last Name"))
is_active = forms.BooleanField(required=False, initial=False, label=_("Activo"), widget=forms.HiddenInput())
class Meta:
model = User
fields = ('username','first_name','last_name','email','password1','password2','is_active')
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
user.is_active = self.cleaned_data['is_active']
if commit:
user.save()
return user
I use the signals in models.py file but you can use it in a signals.py file
#models.py
from django.contrib.auth.models import User
from django.db.models import signals
from django.db import models
from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save
from django.conf import settings
from django.core.mail import send_mail
#signal used for is_active=False to is_active=True
@receiver(pre_save, sender=User, dispatch_uid='active')
def active(sender, instance, **kwargs):
if instance.is_active and User.objects.filter(pk=instance.pk, is_active=False).exists():
subject = 'Active account'
mesagge = '%s your account is now active' %(instance.username)
from_email = settings.EMAIL_HOST_USER
send_mail(subject, mesagge, from_email, [instance.email], fail_silently=False)
#signal to send an email to the admin when a user creates a new account
@receiver(post_save, sender=User, dispatch_uid='register')
def register(sender, instance, **kwargs):
if kwargs.get('created', False):
subject = 'Verificatión of the %s account' %(instance.username)
mesagge = 'here goes your message to the admin'
from_email = settings.EMAIL_HOST_USER
send_mail(subject, mesagge, from_email, [from_email], fail_silently=False)