2012-09-03 49 views
0

問題我有一些問題,validation.When我提交表單時,form.is_valid(): 總是返回。 從表格中刪除所有驗證修改後的形式,即之後,它仍然會返回錯誤(不提交我的形式)與驗證

HTML

{% extends "base.html" %} 
{% block extrahead %} 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js" type="text/javascript"></script> 
    <script> 
    $(function() { 
     $("#id_birthday").datepicker(); 
    }); 
    </script> 
{% endblock %} 
{% block content %} 
<form action="" method="post"> 
{% csrf_token %} 

<div class="register_div"> 

    <p><label for="username">Username:</label></p> 


    <p>{{ form.username }}</p> 
</div> 
<div class="register_div"> 

    <p><label for="email">Email:</label></p> 
    <p>{{ form.email }}</p> 
</div> 
<div class="register_div"> 

    <p><label for="password">Password:</label></p> 
    <p>{{ form.password }}</p> 
</div> 
<div class="register_div"> 

    <p><label for="password1">Verify Password:</label></p> 
    <p>{{ form.password1 }}</p> 
</div> 
<div class="register_div"> 

    <p><label for="birthday">Birthday:</label></p> 
    <p>{{ form.birthday }}</p> 
</div> 
<div class="register_div"> 
    <p><label for="name">Name:</label></p> 
    <p>{{ form.username }}</p> 
</div> 
<p><input type="submit" value="submit" alt="register"/></p> 
</form> 
{% endblock %} 

的forms.py

代碼的代碼
from django import forms 
from django.contrib.auth.models import User 
from django.forms import ModelForm 
from drinker.models import Drinker 

class RegistrationForm(ModelForm): 
     username = forms.CharField(label=(u'User Name')) 
     email  = forms.EmailField(label=(u'Email Address')) 
     password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False)) 
     password1 = forms.CharField(label=(u'Verify Password'), widget=forms.PasswordInput(render_value=False)) 

     class Meta: 
       model=Drinker 
       exclude=('user',) 

     def clean_username(self): 
       username=self.cleaned_data['username'] 
       try: 
         User.objects.get(username=username) 
       except User.DoesNotExist: 
         return username 
       raise forms.ValidationError("The Username is already taken, please try another.") 
     def clean_password(self): 
       password=self.cleaned_data['password'] 
       password1=self.cleaned_data['password1'] 
       if password1 != password: 
        raise forms.ValidationError("The Password did not match, please try again.") 
       return password 

class LoginForm(forms.Form): 
     username = forms.CharField(label=(u'User Name')) 
     password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False)) 

和views.py

def DrinkerRegistration(request): 
    if request.user.is_authenticated(): 
     return HttpResponseRedirect('/profile/') 
    if request.method == 'POST': 
     form = RegistrationForm(request.POST) 
     #return render_to_response('register') 
     if form.is_valid(): 
        user=User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password']) 

        user.save() 
#     drinker=user.get_profile() 
    #    drinker.name=form.cleaned_data['name'] 
    #    drinker.birthday=form.cleaned_data['birthday'] 
    #    drinker.save() 
        drinker=Drinker(user=user,name=form.cleaned_data['name'],birthday=form.cleaned_data['birthday']) 
        drinker.save()  
        return HttpResponseRedirect('/profile/') 
     else: 
        return render_to_response('register.html',{'form':form} , context_instance=RequestContext(request)) 
    else: 
     ''' user is not submitting the form, show them a blank registration form ''' 

     form = RegistrationForm() 
     context={'form':form} 
     return render_to_response('register.html',context , context_instance=RequestContext(request)) 

模型鱈魚的代碼Ë

from django.db import models 
from django.contrib.auth.models import User 
from django.db.models.signals import post_save 
class Drinker(models.Model): 
     user  =models.OneToOneField(User) 
     birthday =models.DateField() 
     name  =models.CharField(max_length=100) 

     def __unicode__(self): 
      return self.name 

#create our user object to attach to our drinker object 

def create_drinker_user_callback(sender, instance, **kwargs): 
     drinker, new=Drinker.objects.get_or_create(user=instance) 
post_save.connect(create_drinker_user_callback, User) 

回答

0

我可以看到兩個可能的原因:

  1. 你的模板中包含的username兩種用法:一次是在頂部,一旦在底部。表單可能會失敗,因爲它不適合單個CharField
  2. 沒有你的模型類很難說,但它可能包含你沒有明確排除的字段?

如果不是選項1,您可以發佈您的Drinker模型嗎?

+0

好吧,我貼我的模型,當我提交我的形式,它擊掌**在KeyError異常的錯誤/註冊/ 「密碼1」 ** – user786

+0

沒有整個堆棧跟蹤這很難說,但我認爲這是您的'ModelForm'具有'password1'字段,而您的基礎模型沒有該字段。 也許在這種情況下'ModelForm'不是你要找的;相反,嘗試使用正常的形式在Django文檔中解釋:https://docs.djangoproject.com/en/1.4/ref/forms/ – publysher

+0

我刪除了password1字段,但仍然不工作 – user786

0

您是否在驗證過程中檢查了錯誤報告?

使用print form.errors並檢查您的控制檯錯誤信息

+0

ya。它總是顯示密碼錯誤在password1.But當我刪除這個字段,並刪除所有驗證仍然表單沒有submit.my視圖如上所示我已經使用了一行'返回render_to_response('註冊') '這行是執行if我評論其餘的代碼意味着方法是後,因此我得出結論認爲'如果form.is_valid():'總是返回false並且它的其他部分被執行 – user786

0

如果你的代碼的更改不會在運行環境反射,看看刪除您* pyc文件。這些有時可以緩存。

對於linux操作系統,您可以從項目的根目錄運行以下命令;

find . -iname "*.pyc" -exec rm -f {} \;