开发者

How to submit image uploads in Django tests?

开发者 https://www.devze.com 2022-12-14 07:18 出处:网络
The Django docs (http://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.Client.post) say to do this:

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'开发者_如何学Pythont work either and behaves the same as passing an empty string.


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号