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问题,但表单在实际网站上运行正常.
读取文件并发送它而不仅仅是一个句柄也不起作用,并且与传递空字符串的行为相同.