我的models.py中有以下代码:
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)
我遇到了无法解决的问题: - 为原始图像(图像字段)正确生成了图像路径,但不适用于缩略图(缩略图字段)。
看起来像:
- img/products/6fddb163-435b-11e3-98fe-843835614698/large/IMG_0171.JPG (适合图像)
- img/products/None/thumb/IMG_0171.JPG.png (对拇指不好)
另外,我不确定管理面板中的路径是否正确生成,因为它们是绝对的(例如:http://localhost:8000/admin/app/product/1/img/products/6fddb163-435b-11e3-98fe-843835614698/large/IMG_0171.JPG
)-我无法访问它们。img 目录是直接在 django 项目目录中创建的,我不知道如何将其更改为在静态目录中创建。
你能帮我解决拇指路径和管理路径的问题吗?