Django forms.FileField validation

I am using file field and text field in my form

class SolutionForm(forms.Form):
    text = forms.CharField(widget=forms.Textarea, required=False)
    file  = forms.FileField(required=False)

      

I also defined a clean method:

def clean(self,*args, **kwargs):
    if (not self.data['file'] or self.data['text']):
         raise forms.ValidationError('Please enter your code in text box or upload an arrpopriate file.')
return self.data['text']

      

but I get the following errors when submitting the form:

"Key 'file' not found in <QueryDict: {u'text': [u''], u'csrfmiddlewaretoken': [u'c52ea10c16620d3ebf0a20f015a3711d'], u'version': [u'C 1.1.1']}>"

      

How can I refer to a file field?

Any help would be appreciated.

Thank,

Pankai.

+3


source to share


1 answer


# inside SolutionForm class
def clean(self):
    if not (self.cleaned_data['file'] or self.cleaned_data['text']):
        raise forms.ValidationError('Please enter your code in text box or upload an appropriate file.')
    return self.cleaned_data

      



+3


source







All Articles