Pankaj Bhambhani
Pankaj Bhambhani

Reputation: 679

Django forms.FileField validation

I am using a filefield and a text field in my form

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

I have 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 when submitting the form i get the following errors:

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

How do I reference file field ?

Any help would be appreciated.

Thanks,

Pankaj.

Upvotes: 4

Views: 7627

Answers (1)

okm
okm

Reputation: 23871

# 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

Upvotes: 2

Related Questions