{
"item_image": [
"The submitted data was not a file. Check the encoding type on the form."
],
"item_thumb": [
"The submitted data was not a file. Check the encoding type on the form."
]
}
我发布的数据是
{
"item_name": "Lural",
"item_image": "/home/prashant/Desktop/suede.png",
"item_thumb": "/home/prashant/Desktop/suede.png",
"item_description": "sd",
"item_mass": 1,
"item_category": "Make Up",
"item_sub_category": "Sub-Feminine",
"item_est_price": "123.12",
"item_wst_price": "120.34"
}
媒体类型应用/ json
@api_view(['GET', 'POST'])
def product_list(request):
if request.method == 'POST':
serializer = ProductSerializer( data=request.data)
# data.encode("base64")
if serializer.is_valid():
serializer.save()
res_msg = {'Success_Message' : 'Created','Success_Code' : 201}
return Response(res_msg)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class Product(models.Model):
item_category_choices = (
('Make Up','Make Up'),
('Skin Care','Skin Care'),
('Fragrance','Fragrance'),
('Personal Care','Personal Care'),
('Hair Care','Hair Care'),
)
item_name = models.CharField(max_length=50)
item_image = models.ImageField()
item_thumb = models.ImageField()
item_description = models.TextField(max_length=200)
item_mass = models.IntegerField()
item_category = models.CharField(max_length=20,choices = item_category_choices)
item_sub_category = models.CharField(max_length=20)
item_est_price = models.DecimalField(max_digits=15,decimal_places=2)
item_wst_price = models.DecimalField(max_digits=15,decimal_places=2)
def __unicode__(self):
return self.item_name or _('Sprint ending %s')% self.item_avg_price
class ProductSerializer(ModelSerializer):
class Meta:
model = Product
fields = ('id','item_name' ,'item_image','item_thumb','item_description','item_mass','item_category',
'item_sub_category','item_est_price','item_wst_price',)
试过很多论坛&第三方包装,但他们不能解决这个问题。 GET工作也很好。
感谢您的时间
答案 0 :(得分:2)
您需要实际打开文件并提交该文件,而不是提交文件"/home/prashant/Desktop/suede.png"
的链接。
例如,这是我必须测试图像提交的测试:
# generate image and open
tmp_file = Image.new('RGB', (3, 3,), 'white')
tmp_file.putpixel((1, 1,), 0)
tmp_file.save(f.name, format='PNG')
_file = open(f.name, 'rb')
data = {'file': _file}
response = api.client.put(url=url, data=data)
答案 1 :(得分:1)
来自Django Docs -
如果您打算允许用户上传文件,则必须确保 用于运行Django的环境配置为使用非ASCII 文件名。如果您的环境配置不正确,您将会这样做 使用文件保存文件时遇到UnicodeEncodeError异常 包含非ASCII字符的名称。
因此,将此方法添加到模型中解决了我的问题:
class Product(models.Model):
item_category_choices = (
('Make Up','Make Up'),
('Skin Care','Skin Care'),
('Fragrance','Fragrance'),
('Personal Care','Personal Care'),
('Hair Care','Hair Care'),
)
item_name = models.CharField(max_length=50,verbose_name='Product Name')
item_image = models.ImageField(verbose_name='Product Image')
item_thumb = models.ImageField(verbose_name='Product Thumb')
item_description = models.TextField(verbose_name='Product Descriptions')
item_mass = models.CharField(max_length=10,verbose_name='Product Weight')
item_category = models.CharField(max_length=20, choices = item_category_choices,verbose_name='Product Category')
item_sub_category = models.CharField(max_length=20,verbose_name='Product Sub Category')
item_est_price = models.DecimalField(max_digits=12,decimal_places=2,verbose_name='East Product Price')
item_wst_price = models.DecimalField(max_digits=12,decimal_places=2,verbose_name='West Product Price')
def __unicode__(self):
return (self.item_name)
def image_img(self):
if self.item_image:
return u'<img src="%s" width="50" height="50" />' % self.item_image.url
else:
return '(Sin imagen)'
image_img.short_description = 'Thumb'
image_img.allow_tags = True
答案 2 :(得分:0)
您应该打开图像并按如下方式发送请求:
with open("/home/prashant/Desktop/suede.png", 'rb') as image:
data = {'item_name': 'Lural',
'item_image': image,
'item_thumb': image,
'item_description': 'sd',
'item_mass': 1,
'item_category': 'Make Up',
'item_sub_category': 'Sub-Feminine',
'item_est_price': '123.12',
'item_wst_price': '120.34'
}
response = api.client.put(url, data, format='multipart')
这应该有效!