Invalid Django Form

I have a form that does not pass the test is_valid(). I created an identical form that passes the test with the same data entry. Not sure why someone will pass, but one will fail.

Here are the forms:

choices = ( (1,'Yes'),(0,'No'),
      )

class ActivitySaveForm(forms.Form):
    name = forms.CharField(
        label=u'Activity Name',
        widget=forms.TextInput(attrs={'size': 64})
    )
    url = forms.URLField(
        label=u'URL',
        widget=forms.TextInput(attrs={'size': 64})
    )
    desc = forms.CharField(
        label=u'Describe it',
                widget=forms.TextInput(attrs={'size': 250})
    )
    created = forms.DateField(
        label=u'Date Entered',initial=datetime.date.today,
    )
    priority = forms.CharField(
        label=u'priority: 1-5',
            widget=forms.TextInput(attrs={'size': 1})
    )
    difficulty = forms.CharField(
        label=u'How hard is it? 1-5',
            widget=forms.TextInput(attrs={'size': 1})
    )
    done = forms.TypedChoiceField(choices=choices, widget=forms.RadioSelect, coerce=int
    )   
    tags = forms.CharField(
        label=u'Tags',required=False,
            widget=forms.TextInput(attrs={'size': 64})
    )
#def __init__(self, *args, **kwargs):
    #super(CircuitForm, self).__init__(*args, **kwargs)

    #for key in self.fields:
        #self.fields[key].required = False


class AcTest(forms.Form):
    name = forms.CharField(
        label=u'Activity Name',
        widget=forms.TextInput(attrs={'size': 64})
    )
    url = forms.URLField(
        label=u'URL',
        widget=forms.TextInput(attrs={'size': 64})
    )
    desc = forms.CharField(
        label=u'Describe it',
                widget=forms.TextInput(attrs={'size': 250})
    )
    created = forms.DateField(
        label=u'Date Entered',initial=datetime.date.today,
    )
    priority = forms.CharField(
        label=u'priority: 1-5',
            widget=forms.TextInput(attrs={'size': 1})
    )
    difficulty = forms.CharField(
        label=u'How hard is it? 1-5',
            widget=forms.TextInput(attrs={'size': 1})
    )
    done = forms.TypedChoiceField(choices=choices, widget=forms.RadioSelect, coerce=int
    )   
    tags = forms.CharField(
        label=u'Tags',required=False,
            widget=forms.TextInput(attrs={'size': 64})
    )

Here is a test and results for both.

>>> data = {'name':'test',
...         'url': 'www.test.com',
...         'desc':'test desc',
...         'created': '01/01/1900',
...         'priority':1,
...         'difficulty':1,
...         'desc':'test desc',
...         'tags':'test desc'}
>>> f=ActivitySaveForm(data)
>>> f.is_valid()
False
>>> f=AcTest(data)
>>> f.is_valid()
True

You do not know how to further diagnose this error and have not found a way to make it work. Thanks for your help in this.

+5
source share
2 answers

I ran your code and for me both forms are not validated. You can simply do this: print f.errorsand you will receive an HTML error message that was generated during the check.

For this, I get:

<ul class="errorlist"><li>done<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

, "done", . 'done':1 data .

, , ... , , , , . , . , , . .

, , .

+8

. - , . (, )

https://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

>>> f = ActivitySaveForm(data)
>>> f.non_field_errors()
>>> field_errors = [ (field.label, field.errors) for field in f] 
+2
source

All Articles