2016-04-23 76 views
1

我有一个由电子邮件和名称字段组成的Django表单。我想验证名称的字符数超过8个。我已经使用了下面的代码。但它不起作用。以Django形式进行字段验证

class SignUpForm(forms.ModelForm): 
    class Meta: 
     model=SignUp 
     fields=('email','name') 
    def emailValidation(self): 

     name=self.cleaned_data.get('name') 
     if len(name) <=8: 
      raise forms.ValidationError("name cannot be less than 8") 

models.py

class SignUp(models.Model): 
    name=models.CharField(max_length=200) 
    email=models.EmailField() 
    timestamp=models.DateTimeField(auto_now_add=True, auto_now=False) 
    updated=models.DateTimeField(auto_now=True,auto_now_add=False) 
    def __unicode__(self): 
     return self.name 

views.py

def home(request): 
    form=SignUpForm(request.POST or None)           
    if form.is_valid():            

     instance=form.save(commit=False) 
     instance.save() 
     print instance.timestamp 
    return render(request, 'home.html',{'form':form}) 
+1

请修复您的缩进。 –

+0

做到了。对不起.. – user2375245

+0

你确定窗体中的缩进与你的实际文件中的缩进相同吗? –

回答

0

您需要为您的验证方法,使用正确的名称。 Django表单将调用格式为clean_<fieldname>的方法。

此外,您似乎对您正在验证的字段感到困惑;您的电子邮件验证方法应该被称为clean_email,并且应该通过form.cleaned_data['email']访问电子邮件值,名称应该被称为clean_name并访问form.cleaned_data['name']

+0

非常感谢你..它已解决。 – user2375245

0

像这样的东西可能会给你一些指导。

class RegistrationForm(forms.ModelForm): 
    """ 
    Form for registering a new account. 
    """ 
    firstname = forms.CharField(label="First Name") 
    lastname = forms.CharField(label="Last Name") 
    phone = forms.CharField(label="Phone") 
    email = forms.EmailField(label="Email") 
    password1 = forms.CharField(label="Password") 
    password2 = forms.CharField(label="Password (again)") 
    min_password_length = 8 

class Meta: 
    model = User 
    fields = ['firstname', 'lastname', 'phone', 'email', 'password1', 'password2'] 

def clean_email(self): 
    email = self.cleaned_data['email'] 
    if User.objects.filter(email=email).exists(): 
     raise forms.ValidationError(u'Email "%s" is already in use! Please log in or use another email!' % email) 
    return email 

def clean_password1(self): 
    " Minimum length " 
    password1 = self.cleaned_data.get('password1', '') 
    if len(password1) < self.min_password_length: 
     raise forms.ValidationError("Password must have at least %i characters" % self.min_password_length) 
    else: 
     return password1 

def clean(self): 
    """ 
    Verifies that the values entered into the password fields match 

    NOTE: Errors here will appear in ``non_field_errors()`` because it applies to more than one field. 
    """ 
    cleaned_data = super(RegistrationForm, self).clean() 
    if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data: 
     if self.cleaned_data['password1'] != self.cleaned_data['password2']: 
      raise forms.ValidationError("Passwords didn't match. Please try again.") 
    return self.cleaned_data 

def save(self, commit=True): 
    user = super(RegistrationForm, self).save(commit=False) 
    user.set_password(self.cleaned_data['password1']) 
    if commit: 
     user.save() 
    return user 
1

在您的SignUpForm中,函数emailValidation中没有返回'name'。另外一个主要的错误是你必须命名函数clean_(field_name)而不是emailValidation。 这应该这样做我猜:

class SignUpForm(forms.ModelForm): 
    class Meta: 
     model=SignUp 
     fields=('email','name') 
    def clean_name(self): 

     name=self.cleaned_data.get('name') 
     if len(name) <=8: 
      raise forms.ValidationError("name cannot be less than 8") 
     return name