Uploaded file always fails validation

421 views
Skip to first unread message

mike.thon

unread,
Oct 19, 2009, 10:36:07 AM10/19/09
to Django users
Hi all - I am quite new to python and Django but I'm starting to learn
my way around. I am writing a form for uploading files but the form
always seems to fail the form.is_valid() test. if I print the form
errors using:

errors = form.errors
print errors

I get this string:

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

I can read the file name and file contents from the request.FILES
object and the data is there but is_valid() seems to always return
False. I guess there must be a default clean() method on uploaded
files that always returns false unless I override it? I think I can
write my own validator method and put it in clean_<fieldname>() in the
form subclass but I would like to find out why the error is happening
before I do that.

BTW, I can upload the same file through the admin interface and I do
not get the error.
TIA

mike.thon

unread,
Oct 19, 2009, 11:30:52 AM10/19/09
to Django users
Well, I found the problem - When binding the POST data to the form,
you also need to specify the form file data (request.FILES)
http://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files-to-a-form

Thanks anyway!
Reply all
Reply to author
Forward
0 new messages