- 论坛徽章:
- 0
|
代码全部来自《learning website development with django》
RegistrationForms的定义
- class RegistrationForm(forms.Form):
- username = forms.CharField(label='Username',max_length = 30)
- email = forms.EmailField(label='Email')
- password1 = forms.CharField(label='Password',widget=forms.PasswordInput())
- password2 = forms.CharField(label='Password(Again)',widget=forms.PasswordInput())
-
- def clean_password2(self):
- if 'password1' in self.cleaned_data:
- password1 = self.cleaned_data['password1']
- password2 = self.cleaned_data['password2']
- if password1 == password2:
- return password2
- raise forms.ValidationError('Passwords do not match')
-
- def clean_username(self):
- username = self.cleaned_data['username']
- if not re.search(r'^\w+$',username):
- raise forms.ValidationError('Username can only contain alphanumeric characters and the underscore.')
- try:
- User.objects.get(username=username)
- except ObjectDoesNotExist:
- return username
- raise forms.ValidationError('Username is already taken.')
复制代码
我在python manage.py shell里实验了一下:
- >>> from bookmarks.forms import RegistrationForm as Form
- >>> user = Form({'username':'hhh','password1':'123123','password2':'123123','email':'123@123.com'})
- >>> user.is_valid()
- True
- >>> user.cleaned_data
- {'username': None, 'password1': u'123123', 'password2': u'123123', 'email': u'123@123.com'}
复制代码
我不明白为什么user的'username'字段会变成None
我屏蔽了一些代码,实验出如果调用了clean_username之后,username就会变成None,有没有谁有遇到过这种问题的,麻烦指点一下,很急,谢谢了阿!
[ 本帖最后由 zhaoxix9527 于 2009-4-20 16:13 编辑 ] |
|