form.py 761 B

123456789101112131415161718192021222324252627282930
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from .models import Profile
  4. class UserLoginForm(forms.Form):
  5. username = forms.CharField()
  6. password = forms.CharField()
  7. class UserRegisterForm(forms.ModelForm):
  8. password = forms.CharField()
  9. password2 = forms.CharField()
  10. class Meta:
  11. model = User
  12. fields = ('username', 'email')
  13. def clean_password2(self):
  14. data = self.cleaned_data
  15. if data.get('password') == data.get('password2'):
  16. return data.get('password')
  17. else:
  18. raise forms.ValidationError('密码输入不一致,请重试')
  19. class ProfileForm(forms.ModelForm):
  20. class Meta:
  21. model = Profile
  22. fields = ('phone', 'avatar', 'bio')