Django表单验证失败

时间:2012-03-28 01:25:53

标签: django

在以下代码中,

form.is_valid始终设置为false。如何实现代码以检查其错误原因。

我已将代码提供给下面的forms.py,model.py和views.py。

表单定义表单 视图处理表单 模型定义了模型

forms.py

    class VolunteerForm(forms.ModelForm):
        volposition = forms.CharField(label="VolunteerPosition",widget=forms.Textarea(attrs={'cols':30,'rows':1}))
        roledesc = forms.CharField(label="Role Description",widget=forms.Textarea(attrs={'cols':30,'rows':5}))
        noofhours = forms.CharField(widget=forms.Select(choices=NO_OF_HRS_MONTH),max_length=2)
        Qualreqt  = forms.CharField(label="Qualifications and Requirements",widget=forms.Textarea(attrs={'cols':30,'rows':5}))
        Duration      = forms.CharField(widget=forms.Select(choices=NO_OF_HRS),max_length=2)
        Durationyrmon = forms.CharField(widget=forms.Select(choices=YR_MONTH),max_length=10)
        posstatus     = forms.CharField(widget=forms.Select(choices=POS_STATUS),max_length=1)
        teamrelation  = forms.CharField(max_length=50)


        class Meta:
            model = Volunteer        

views.py

    def volunteersignupform_2_model(form):
       if not form.is_valid():
           return None
       data = form.cleaned_data
       signup = Contact(email=data['email'],
                        phone=data['phone'],
                        first_name=data['first_name'],
                        last_name=data['last_name'],
                        location=data['location'],
                        other_location=data['other_location'],
                        new_ac=data['new_ac'],
                        int_volunteer=data['int_volunteer'],
                        int_projects=data['int_projects'],
                        int_fundraise=data['int_fundraise'],
                        int_it=data['int_it'],
                        int_programs=data['int_programs'],
                        int_marketing=data['int_marketing'],
                        age=data['age'],
                        occupation=data['occupation'],
                        intro_source=data['intro_source'],
                        comments=data['comments'])
        signup.save()
        # The next line is needed for signup.get_location_display() function to
        # work
        signup = Contact.objects.get(id=signup.id)
        return signup

models.py

    NO_OF_HRS = (
        ('1','1'),
        ('2','2'),
        ('3','3'),
        ('4','4'),
        ('5','5'),
        ('6','6'),
        ('7','7'),
        ('8','8'),
        ('9','9'),
        ('10','10'),
        ('11','11'),
        ('12','12'),
        )

    YR_MONTH = (
    ("Y", "Year"),
    ("M", "Month"),
    )

    POS_STATUS = (
    ("A", "Active"),
    ("C", "Closed"),
     )

    NO_OF_HRS_MONTH = (
    ("10","10"),
    ("20","20"),
    ("30","30"),
    ("40","40"),
    ("50","50"),
    ("60","60"),
    ("70","70"),
    ("80","80"),
    ("90","90"),
     )


    class Volunteer(models.Model):

        datecreated   = models.DateTimeField()
        volposition   = models.CharField(max_length=100)
        roledesc      = models.CharField(max_length=400)
        noofhours     = models.CharField(choices=NO_OF_HRS_MONTH,max_length=2)
        Qualreqt      = models.CharField(max_length=500)
        Duration      = models.CharField(choices=NO_OF_HRS,max_length=2)
        Durationyrmon = models.CharField(choices=YR_MONTH,max_length=10)
        posstatus     = models.CharField(choices=POS_STATUS,max_length=1)
        teamrelation  = models.CharField(max_length=50)

0 个答案:

没有答案
相关问题