【发布时间】:2014-10-16 10:01:41
【问题描述】:
我在 Django Rest 中实现了一个具有 ImageField 的简单模型。 它适用于 Django Rest 的 Browsable API。
但是,当我尝试编写测试用例来发布 JSON 时,它会引发错误 UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte
您可以在下面查看测试代码。基本上,我打开测试图像并将其作为 JSON 参数传递 我不确定为什么它不能编码。
test_image_filename = os.path.join('/vagrant/', 'test_images', 'test_image1.jpg')
with open(test_image_filename) as image_file:
data = {
"location": "123, 123",
"location_name": "location1",
"date": datetime.datetime.now().__str__(),
"max_attendee": 10,
"description": "test",
"image": image_file,
"tags": []
}
response = self.client.post('/events/', data)
print response
【问题讨论】:
-
我认为应该是文件内容,而不是打开的文件对象
标签: json django file-upload django-rest-framework