V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Gothack
V2EX  ›  Python

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

  •  
  •   Gothack · 2011-12-23 17:24:12 +08:00 · 3638 次点击
    这是一个创建于 4532 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大家帮忙看下,注册表单代码如下,现在的问题是注册不管成功与否都返回注册成功页面,但不符合下面定义条件的无法插入数据库,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
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2925 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:21 · PVG 19:21 · LAX 04:21 · JFK 07:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.