Django Registration with Email
October 1, 2014
I’m a huge believer in simple interfaces, and not overwhelming users with form fields. While not always the best option, for most of our projects, it’s much cleaner to have a user register with just an email and a password.
Here is our quick and dirty implementation for extending the user form from the django.contrib.auth module to work for our use case.
Form
We want to extend the base UserCreationForm found in django.contrib.auth.forms. To do so, we need to:
- Extend the UserCreationForm class.
- Add in the email field.
- Specify the form fields.
- Pop (remove) the username field on init.
- Alter the save function so the email is added to the user record, and the email is recorded to the username field
forms.py:
View
In the view.py file, we need to check the post for valid data, using our new form. If the submitted data is valid, we save the user and redirect to the homepage. Otherwise, return to the registration page:
views.py:
Template
register.html:
Closing remarks
As we stated earlier, this is a quick and dirty implementation, but it should give you a starting point. You will also need to handle special characters that are illegal for username usage, and username length restrictions, among other things.