Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit image uploads in Django tests?

Tags:

django

testing

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.

like image 683
Kyle MacFarlane Avatar asked Dec 20 '09 20:12

Kyle MacFarlane


1 Answers

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.

like image 134
Kyle MacFarlane Avatar answered Sep 30 '22 19:09

Kyle MacFarlane