大家帮忙看下这个django注册表单的valicationerror为什么无法执行

2011 年 12 月 23 日
 Gothack
大家帮忙看下,注册表单代码如下,现在的问题是注册不管成功与否都返回注册成功页面,但不符合下面定义条件的无法插入数据库,validationerror上面的print语句都能在后台输出,求大神赐教

#view.py的注册函数:
if request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
user = User.objects.create_user(username = form.cleaned_data['username'],
password = form.cleaned_data['password2'],
email = form.cleaned_data['aemail'])
print request.POST['username'],request.POST['password2']
return render_to_response('reg_success.html',context_instance=RequestContext(request,processors=[processor,messages]))

#form.py
class RegisterForm(forms.Form):
aemail = forms.EmailField(label='邮箱')
username = forms.CharField(label='用户名',max_length=128)
password1 = forms.CharField(label='密码',widget=forms.PasswordInput())
password2 = forms.CharField(label='确认密码',widget=forms.PasswordInput())
def clean_username(self):
username = self.cleaned_data['username']
if not re.search(r'\w+$',username):
print '用户名只能包含字母、数字和下划线!'
raise forms.ValidationError('用户名只能包含字母、数字和下划线!')
user=User.objects.filter(username=username)
if user:
print '用户名已存在!'
raise forms.ValidationError('用户名已存在!')
return username
def clean_email(self):
email = self.cleaned_data['aemail']
mail=User.objects.filter(email=email)
if mail:
print '该邮箱已经注册!'
raise forms.ValidationError('该邮箱已经注册!')
return email
def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2:
if password1 != password2:
print 'The two password fields didnt match.'
raise forms.ValidationError("The two password fields didn't match.")
return password2
4178 次点击
所在节点    Python
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/24151

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX