You are going to need this in case you want to allow users creating an account to input more than just their username and password.
Import everything you need: forms.py
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
And your class that uses the UserCreationForm class (still in forms.py)
class RegisterForm(UserCreationForm):
# declare the fields you will show
username = forms.CharField(label="Your Username")
# first password field
password1 = forms.CharField(label="Your Password")
# confirm password field
password2 = forms.CharField(label="Repeat Your Password")
email = forms.EmailField(label = "Email Address")
first_name = forms.CharField(label = "Name")
last_name = forms.CharField(label = "Surname")
# this sets the order of the fields
class Meta:
model = User
fields = ("first_name", "last_name", "email", "username", "password1", "password2", )
# this redefines the save function to include the fields you added
def save(self, commit=True):
user = super(UserCreateForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
user.first_name = self.cleaned_data["first_name"]
user.last_name = self.cleaned_data["last_name"]
if commit:
user.save()
return user