Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from yourapp.models import Profile
  4.  
  5. class SignUpForm(forms.ModelForm):
  6. username = forms.CharField(
  7. required=True,
  8. widget=forms.TextInput()
  9. )
  10. email = forms.EmailField(
  11. required=True,
  12. widget=forms.EmailInput()
  13. )
  14. password = forms.CharField(
  15. widget=forms.PasswordInput()
  16. )
  17. password2 = forms.CharField(
  18. label=_("Password Confirmation"),
  19. widget=forms.PasswordInput()
  20. )
  21. error_message = {
  22. 'password_mismatch': _("The two password didn't match."),
  23. }
  24.  
  25. class Meta:
  26. model = Profile
  27. fields = '__all__'
  28. exclude = ['user',]
  29.  
  30. def clean_password2(self):
  31. password1 = self.cleaned_data.get('password')
  32. password2 = self.cleaned_data.get('password2')
  33. if password1 and password2 and password1 != password2:
  34. raise forms.ValidationError(
  35. self.error_message['password_mismatch'],
  36. code='password_mismatch',
  37. )
  38. return password2
  39.  
  40. def save(self, commit=True):
  41. user = super(SignUpForm, self).save(commit=False)
  42. user_registration = User.objects.create_user(
  43. username=self.cleaned_data['username'],
  44. password=self.cleaned_data['password'],
  45. email=self.cleaned_data['email']
  46. )
  47. if commit:
  48. user_registration.save()
  49. return user_registration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement