0

I have this form class in forms.py in my Django application:

class EmailForm(EmailFormWithoutAttachment):
    attachment = forms.FileField()

I would like to access the file uploaded in forms.py for validation via a clean function to validate its name.

def clean_attachment(self):
    if self.data['attachment'].name == "someFileName.txt":
        raise forms.ValidationError('This file is not allowed.')
    return self.data['attachment']

However, the problem is that an error notes that "attachment" is not found in "QueryDict." I am binding request.FILES to the submission form. I am also using enctype="multipart/form-data" on my forms. I was wondering what is the proper way to access FileField data in forms.py.

Thank you.

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206

1 Answers1

4

Use self.cleaned_data['attachment'] instead of self.data['attachment'].

chandsie
  • 2,865
  • 3
  • 27
  • 37
  • Thanks for the suggestion, but using self.cleaned_data['attachment'] gives me this error: "KeyError at /email/ 'attachment' " Could it be that I am not passing the file data into the form? – dangerChihuahua007 Jun 24 '11 at 03:39
  • I'm not on a computer right now that i can test this on, but can you try using self.cleaned_data.get('attachment') and see how that pans out? Also, check for possible spelling errors! I'll try to get a better answer as soon as i can get on my other computer. – chandsie Jun 24 '11 at 05:14
  • Also can you post the definition of the `EmailFormWithoutAttachment` class please? – chandsie Jun 24 '11 at 05:17