4👍
✅
From Django Docs –
If you intend to allow users to upload files, you must ensure that the
environment used to run Django is configured to work with non-ASCII
file names. If your environment isn’t configured correctly, you’ll
encounter UnicodeEncodeError exceptions when saving files with file
names that contain non-ASCII characters.
Thus adding this method to the model solved my problem :
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👍
Instead of submitting a link to a file "/home/prashant/Desktop/suede.png"
, you need to actually open the file and submit that instead.
For example, here is a test I have to test image submission:
# 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)
👤djq
- How does a python web server overcomes GIL
- Setup postgres in Github Actions for Django
- Getting data from multiple databases with same tablenames in django
1👍
You should open the image and send the request as follows:
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')
This should work!
Source:stackexchange.com