How to send images to Django tests?

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 this, the error message "The file is empty" appears in the field. It smells like a PIL problem, but the form works fine on the site itself.

Reading the file and sending it instead of the descriptor also does not work and behave the same way as passing an empty line.

+6
django testing
source share
2 answers

OK, I get it. I used the same dummy image for multiple fields, and Django did not indicate a reset pointer after checking the first field.

Also, the example in the documents does not show that images need to be opened in binary mode.

+6
source share

I think open expects a file path as to where it is being called from.

I'm not sure where it will be when the test is running, but maybe try with an absolute path and see if it works?

0
source share

All Articles