Django
3
Вклад в тег
class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(
label='passwd',
widget=forms.PasswordInput(attrs={
'style':'margin:10px; padding:10px;height:40px',
'class':'form-control col-sm-8',
'placeholder': 'Password please'
})
)
password2 = forms.CharField(
label='double passwd',
widget=forms.PasswordInput(attrs={
'style':'margin:10px; padding:10px;height:40px',
'class':'form-control col-sm-8',
'placeholder': 'Double password please'
})
)
def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError('Passwd and double passwd error')
return password2
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1']) # важно тут передавать password1 или 2. Но не password как в примере выше
if commit:
user.save()
return user
class Meta:
model = get_user_model()
fields = ('email', 'lastname', 'firstname')
widgets = {
'email': forms.TextInput(attrs={
'style':'margin:10px; padding:10px;height:40px',
'class':'form-control col-sm-8',
'placeholder': 'E-mail please'
}),
'lastname': forms.TextInput(attrs={
'style':'margin:10px; padding:10px;height:40px',
'class':'form-control col-sm-8',
'placeholder': 'Lastname please'
}),
'firstname': forms.TextInput(attrs={
'style':'margin:10px; padding:10px;height:40px',
'class':'form-control col-sm-8',
'placeholder': 'Firstname please'
}),
}
def registerPost(request):
if request.method == "POST":
user_form = UserCreationForm(request.POST)
if user_form.is_valid():
new_user = user_form.save(commit=False)
new_user.set_password(user_form.cleaned_data['password1'])
new_user.avatar = "fotos/no_image_dummy.png"
new_user.save()
return HttpResponseRedirect("/login")
else:
user_form = UserCreationForm()
return render(request, 'register.html', {'form':user_form})