Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. class UserForm(forms.ModelForm):
  2. password=forms.CharField(widget=forms.PasswordInput())
  3.  
  4. class Meta:
  5. model=User
  6. fields=('username','email','password')
  7.  
  8. class UserProfileForm(forms.ModelForm):
  9. YESNO_CHOICES = (('male', 'male'), ('female', 'female'))
  10. sex = forms.TypedChoiceField(choices=YESNO_CHOICES, widget=forms.RadioSelect)
  11. FAVORITE_COLORS_CHOICES=(('red','red'),('blue','blue'))
  12. favorite_colors = forms.MultipleChoiceField(required=False,widget=forms.CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)
  13. dob = forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'),
  14. input_formats=('%d/%m/%Y',))
  15.  
  16. class Meta:
  17.  
  18. model=UserProfile
  19. fields=('phone','picture','sex','favorite_colors','dob')
  20.  
  21. def register(request):
  22. registered = False
  23. if request.method == 'POST':
  24. user_form = UserForm(data=request.POST)
  25. profile_form = UserProfileForm(data=request.POST)
  26.  
  27.  
  28.  
  29. if user_form.is_valid() and profile_form.is_valid():
  30. user = user_form.save(commit=False)
  31. user.set_password(user.password)
  32. user.save()
  33. profile = profile_form.save(commit=False)
  34. profile.user = user
  35. if 'picture' in request.FILES:
  36. profile.picture = request.FILES['picture']
  37. profile.save()
  38. registered = True
  39. else:
  40. print user_form.errors, profile_form.errors
  41. else:
  42. user_form = UserForm()
  43. profile_form = UserProfileForm()
  44.  
  45. return render(request,
  46. 'mysite/register.html',
  47. {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
  48.  
  49. class UserForm(forms.ModelForm):
  50. password=forms.CharField(widget=forms.PasswordInput())
  51. confirm_password=forms.CharField()
  52. class Meta:
  53. model=User
  54. fields=('username','email','password')
  55.  
  56. def clean(self):
  57. cleaned_data = super(UserForm, self).clean()
  58. password = cleaned_data.get("password")
  59. confirm_password = cleaned_data.get("confirm_password")
  60.  
  61. if password != confirm_password:
  62. raise forms.ValidationError(
  63. "password and confirm_password does not match"
  64. )
  65.  
  66. class UserForm(forms.Form):
  67. password = forms.CharField(widget=forms.PasswordInput())
  68. password_confirm = forms.CharField(widget=forms.PasswordInput())
  69.  
  70. class Meta:
  71. model = User
  72. fields=('username','email','password')
  73.  
  74. if user_form.is_valid() and profile_form.is_valid() and user_form.cleaned_data['password'] == user_form.cleaned_data['password_confirm']:
  75. ...
  76. elif user_form.data['password'] != user_form.data['password_confirm']:
  77. user_form.add_error('password_confirm', 'The passwords do not match')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement