Django 带有缩略图的ImageField和生成的上传路径
我的models.py中有以下代码:Django 带有缩略图的ImageField和生成的上传路径,django,django-models,django-admin,django-settings,Django,Django Models,Django Admin,Django Settings,我的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 Prod
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项目目录中创建的,我不知道如何将其更改为在静态目录中创建
您能帮我解决拇指路径和管理员路径的问题吗?我已经解决了其中一个问题:我必须设置正确的媒体URL和媒体根路径。您可以为自己的问题添加自己的答案并接受它