Note that there are some explanatory texts on larger screens.

plurals
  1. POImageField with thumbnail and generated upload_to path
    primarykey
    data
    text
    <p>I have following code in my models.py:</p> <pre><code>def product_upload_to(instance, filename): return 'img/products/%s/large/%s' % (instance.uuid, filename,) def thumb_upload_to(instance, filename): return 'img/products/%s/thumb/%s' % (instance.uuid, filename,) class Product(BaseModel): company = models.ForeignKey(Company, null=True, blank=True) title = models.CharField(max_length=128) description = models.TextField() category = models.ForeignKey(ProductCategory, null=True, blank=True) price = models.DecimalField(max_digits=5,decimal_places=2,verbose_name="Cena") image = models.ImageField(upload_to=product_upload_to,null=True,blank=True) thumb = models.ImageField(upload_to=thumb_upload_to,null=True,blank=True) def save(self, force_update=False, force_insert=False, thumb_size=(120,120)): image = Image.open(self.image) image.thumbnail(thumb_size, Image.ANTIALIAS) temp_handle = StringIO() image.save(temp_handle, 'png') temp_handle.seek(0) # rewind the file suf = SimpleUploadedFile(os.path.split(self.image.name)[-1], temp_handle.read(), content_type='image/png') self.thumb.save(suf.name+'.png', suf, save=False) super(Product, self).save(force_update, force_insert) </code></pre> <p>I ran into issues, which I'm not able to fix: - image path is generated properly for original image (image field) but not for the thumbnail (thumb field). </p> <p>It looks like:</p> <ul> <li>img/products/6fddb163-435b-11e3-98fe-843835614698/large/IMG_0171.JPG <strong>(good for image)</strong></li> <li>img/products/None/thumb/IMG_0171.JPG.png <strong>(bad for thumb)</strong></li> </ul> <p>Also, I am not sure if paths in admin panel are generated properly, because they are absolute (example: <code>http://localhost:8000/admin/app/product/1/img/products/6fddb163-435b-11e3-98fe-843835614698/large/IMG_0171.JPG</code>) - and I can't reach them. img directory is being created directly in django project directory, I don't know how to change this to be created within static directory.</p> <p>Could you help me to solve issue with thumb path and admin path?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload