这是一个创建于 4664 天前的主题,其中的信息可能已经有所发展或是发生改变。
大家帮忙看下,注册表单代码如下,现在的问题是注册不管成功与否都返回注册成功页面,但不符合下面定义条件的无法插入数据库,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