tags:

views:

67

answers:

2

The Django docs (http://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.Client.post) say to do this:

>>> c = Client()
>>> f = open('wishlist.doc')
>>> c.post('/customers/wishes/', {'name': 'fred', 'attachment': f})
>>> f.close()

But when I do that the field has the error message "The submitted file is empty." That smells like a PIL issue but the form works fine on the actual site.

Reading the file and sending that instead of just a handle doesn't work either and behaves the same as passing an empty string.

A: 

I think open expects a file path relative to where it’s being called from.

I’m not sure where that would be when a test is being run, but maybe try with an absolute path and see if it works?

Paul D. Waite
I am using an absolute path and the image opens fine.
Kyle MacFarlane
+1  A: 

OK I figured it out. I was using the same dummy image for multiple fields and Django doesn't reset the pointer after validating the first field.

Also the example in the docs doesn't show that images need to be opened in binary mode as well.

Kyle MacFarlane
I hope you serched for/opened a ticket to fix the example in docs ;)
SmileyChris