Nick Nick - 8 months ago 73
Python Question

Django Registration form Verification Email

I originally started out by using django-registration to implement registration email verification. I've implemented a custom login/registration form to my django app to allow users to login with a unique email address. However, doing so, overrode my django-registration workflow.

Ideally i would like to send the user a verification email after the user registers - instead of redirecting them to the login page. I'm not sure if this has something to do with the file or not.

class AccountUserManager(UserManager):
def _create_user(self, username, email, password,
is_staff, is_superuser, **extra_fields):
Creates and saves a User with the given username, email and password.
now =
if not email:
raise ValueError('The given username must be set')

email = self.normalize_email(email)
user = self.model(username=email, email=email,
is_staff=is_staff, is_active=True,
date_joined=now, **extra_fields)

return user

class User(AbstractUser):
# now that we've abstracted this class we can add any
# number of custom attribute to our user class

# in later units we'll be adding things like payment details!

objects = AccountUserManager()

class UserRegistrationForm(UserCreationForm):
email = forms.EmailField(
widget=forms.EmailInput(attrs={'placeholder': 'Email Address'})
password1 = forms.CharField(
widget=forms.PasswordInput(attrs={'placeholder': 'Password'})

password2 = forms.CharField(
widget=forms.PasswordInput(attrs={'placeholder': 'Confirm Password'})

class Meta:
model = User
fields = ['email', 'password1', 'password2']
exclude = ['username']

def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')

if password1 and password2 and password1 != password2:
message = "Passwords do not match"
raise ValidationError(message)

return password2

def save(self, commit=True):
instance = super(UserRegistrationForm, self).save(commit=False)

# automatically set to email address to create a unique identifier
instance.username =

if commit:

return instance

def register(request, register_form=UserRegistrationForm):
if request.method == 'POST':
form = register_form(request.POST)
if form.is_valid():

user = auth.authenticate(email=request.POST.get('email'),

if user:
messages.success(request, "You have successfully registered")
return redirect(reverse('profile'))

messages.error(request, "unable to log you in at this time!")

form = register_form()

args = {'form':form}

return render(request, 'register.html', args)

Answer Source

I'm not sure if you still want to use django-registration or not?

If you still want to use django-registration, HMAC authentification is documented here.

if not you need to send a mail by yourself. (e.g. with django.core.mail) before returning the rendered template or a redirect.