SimpleUploadedFile 不会 POST
SimpleUploadedFile won't POST
我想使用 Client 将字典传递给函数。看起来像这样:
response = self.client.post(
'/upload_image/', {'image': image, 'tags': ['orion', ]})
在我看来,它正在向我发布数据:
print(request.POST)
image = request.POST['image']
tags = reguest.POST['tags']
request.POST['image'] 上存在 MultiValueDictKeyError。
print(request.POST)
表明字典看起来像:
<QueryDict: {'tags': ['orion']}>
图像对象是:
image = SimpleUploadedFile(
'kitties.png', b'kitties_in_boxes', 'image/png')
这是关于图像对象的,因为当我将其他东西作为图像(例如字符串)传递时,它运行良好。
我想还有另一种方法可以测试上传图片功能,但有人知道为什么这不起作用吗?
Django 将上传的文件与其他提交的数据分开。 tags
键在 request.POST
中存在,但 image
应该在 request.FILES
中。据我所知,您正在做的是使用测试客户端测试文件上传的正确方法。
我想使用 Client 将字典传递给函数。看起来像这样:
response = self.client.post(
'/upload_image/', {'image': image, 'tags': ['orion', ]})
在我看来,它正在向我发布数据:
print(request.POST)
image = request.POST['image']
tags = reguest.POST['tags']
request.POST['image'] 上存在 MultiValueDictKeyError。
print(request.POST)
表明字典看起来像:
<QueryDict: {'tags': ['orion']}>
图像对象是:
image = SimpleUploadedFile(
'kitties.png', b'kitties_in_boxes', 'image/png')
这是关于图像对象的,因为当我将其他东西作为图像(例如字符串)传递时,它运行良好。
我想还有另一种方法可以测试上传图片功能,但有人知道为什么这不起作用吗?
Django 将上传的文件与其他提交的数据分开。 tags
键在 request.POST
中存在,但 image
应该在 request.FILES
中。据我所知,您正在做的是使用测试客户端测试文件上传的正确方法。