tags:

views:

68

answers:

1

I have a form that allows users to upload text AND a file. However, I'd like to make it valid even if the user doesn't upload the file (file is optional). However, in Django, it is not allowing me to get past "clean(self)". I just want it simple--if text box, pass. If no text , return error.

class PieceForm(forms.Form):
        text = forms.CharField(max_length=600)
        file = forms.FileField()
        def clean(self):
            cleaned_data = self.cleaned_data
            text = cleaned_data.get('text')
            file = cleaned_data.get('file')
            return cleaned_data

In my views...

form = PieceForm(request.POST, request.FILES)
if form.is_valid():
    print 'It's valid!' ........this only prints if there is a file!
+4  A: 

You must set required=False for the fields which are optional as noted in the documentation

In your case, the following line should do the trick:

    file = forms.FileField(required=False)
tux21b
Thank you, missed that part.
TIMEX
+++1 :-) aus .at
Flavius