I've been trying to extend the default UserCreationForm
to include an email, first and last names. This appears to be quite a common topic on SO however the logic error I am getting I have still not found a solution to.
Here is my code:
forms.py
class RegistrationForm(UserCreationForm):
first_name = forms.CharField(required=True, label="First name:", max_length=100)
last_name = forms.CharField(required=True, label="Last name:", max_length=100)
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
def save(self, commit=True):
user = super(RegistrationForm, 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
views.py
def register_user(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
return redirect('/reg_interests/')
args = {}
args['form'] = RegistrationForm()
return render(request, 'login/registration.html', args)
The views and form technically work in the sense that I don't see an error message, however upon user.save()
the first and last names are not saved to the database (but the email is).
I know that each of email
, first_name
and last_name
are fields that exist in the default Django User object so the field not being there is not the problem.
How come the extended email
will save to the database but the first_name
and last_name
will not?
Note: I am using the newly released Django 1.10 so I don't know if that has anything to do with it.