Reputation: 2212
I create a new model with additional fields for the User
.
models.py:
class UserProfile(models.Model):
user = models.OneToOneField(User)
email = models.CharField(max_length=50)
birthday = models.DateField(u'Birthday', blank=True, null=True)
avatar = models.ImageField(Avatar', upload_to='profile/avatar', blank=True)
def __unicode__(self):
return self.user
Then I create registration form for users:
class RegistrationForm(ModelForm):
username = forms.CharField(label=(u'Enter Your Username'))
email = forms.EmailField(label=(u'Enter Your E-mail'))
password = forms.CharField(label=(u'Enter Password'), widget=forms.PasswordInput(render_value=False))
password1 = forms.CharField(label=(u'Verify Your Password'), widget=forms.PasswordInput(render_value=False))
class Meta:
model = UserProfile
exclude = ('user',)
def clean_email(self):
email = self.cleaned_data['email']
try:
User.objects.get(email=email)
except User.DoesNotExist:
return email
raise forms.ValidationError("User with same e-mail is already exist, please type another email")
def clean_password(self):
password = self.cleaned_data['password']
password1 = self.cleaned_data['password1']
if not password1:
raise forms.ValidationError("You have ti verify your password")
if password != password1:
raise forms.ValidationError("Your passwords doesn't match")
return password1
And this is my view for registration page:
def UserProfileRegistration(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/info/')
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password=form.cleaned_data['password'])
user.save()
user_profile = UserProfile(user=user)
user_profile.save()
return HttpResponseRedirect('/info/')
else:
return render(request, 'profiles/registration.html', {'form':form})
else:
form = RegistrationForm()
context = {'form':form}
return render (request, 'profiles/registration.html', context)
It doesn't matter if i fill passwords fields right or wrong but i have the same error KeyError at /registration/
, Exception Value: 'password1'
. Thanks for your answer.
Upvotes: 0
Views: 1223
Reputation: 255
Django User already has email no need to add(see https://docs.djangoproject.com/en/1.9/topics/auth/default/#user-objects)
class UserProfile(models.Model):
user = models.OneToOneField(User)
birthday = models.DateField(u'Birthday', blank=True, null=True)
avatar = models.ImageField(Avatar', upload_to='profile/avatar', blank=True)
def __unicode__(self):
return self.user
Besides, you also need to add clean() for password1 as password in forms.
Upvotes: 1
Reputation: 599600
password1
is not guaranteed to have been added to cleaned_data
by the time clean_password()
is called. For any validation that requires the presence of more than one field, you should do it in the general clean()
method.
Upvotes: 1
Reputation: 3378
For model forms if you specify a non model field like password1 you have to mention the fields attribute in the form meta class.
A model form will only include the existing model fields. Add this in the model form meta class:
class Meta:
model = UserProfile
fields = ('username', 'email', 'password', 'password1')
Upvotes: 1