3
我有一個文件上傳其作品完美,但我想它,所以我做了以下....PIL - 提交的文件是空的測試用例
def test_post_ok(self):
image = Image.new('RGB', (100, 100)
tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg')
image.save(tmp_file)
payload = {
"name": "Test",
"thumbnail_image": tmp_file
}
api = APIClient()
api.credentials(Authorization='Bearer ' + self.token)
response = api.post(url, payload, format='multipart')
但是寫一個測試,測試給出錯誤...
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1024x768 at 0x108A5DCF8>
{'thumbnail_image': [u'The submitted file is empty.']}
我假設我沒有正確地做到這一點,如果不是爲什麼呢?
之前重新打開該文件,你在使用庫APIClient()?我的猜測是你應該將文件內容轉換爲Base64,然後才能使用API發送它。但是這當然取決於你的APIClient()是否爲你做這件事。 –
@WouterKleinHeerenbrink我使用''rest_framework.test'' – Prometheus