Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django import forms
- from django.contrib.auth.models import User
- from yourapp.models import Profile
- class SignUpForm(forms.ModelForm):
- username = forms.CharField(
- required=True,
- widget=forms.TextInput()
- )
- email = forms.EmailField(
- required=True,
- widget=forms.EmailInput()
- )
- password = forms.CharField(
- widget=forms.PasswordInput()
- )
- password2 = forms.CharField(
- label=_("Password Confirmation"),
- widget=forms.PasswordInput()
- )
- error_message = {
- 'password_mismatch': _("The two password didn't match."),
- }
- class Meta:
- model = Profile
- fields = '__all__'
- exclude = ['user',]
- def clean_password2(self):
- password1 = self.cleaned_data.get('password')
- password2 = self.cleaned_data.get('password2')
- if password1 and password2 and password1 != password2:
- raise forms.ValidationError(
- self.error_message['password_mismatch'],
- code='password_mismatch',
- )
- return password2
- def save(self, commit=True):
- user = super(SignUpForm, self).save(commit=False)
- user_registration = User.objects.create_user(
- username=self.cleaned_data['username'],
- password=self.cleaned_data['password'],
- email=self.cleaned_data['email']
- )
- if commit:
- user_registration.save()
- return user_registration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement