如何在Django测试中提交图像上传?

Kyl*_*ane 6 testing django

Django文档(http://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.Client.post)说这样做:

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

但是,当我这样做时,该字段有错误消息"提交的文件为空".这有点像PIL问题,但表单在实际网站上运行正常.

读取文件并发送它而不仅仅是一个句柄也不起作用,并且与传递空字符串的行为相同.

Kyl*_*ane 6

好的,我明白了.我在多个字段使用相同的虚拟图像,Django在验证第一个字段后没有重置指针.

此外,文档中的示例也未显示图像也需要以二进制模式打开.