【问题标题】:Django Imagekit overwrite cachefile_name?Django Imagekit覆盖cachefile_name?
【发布时间】:2014-06-09 16:22:33
【问题描述】:

我正在尝试覆盖模块 django-imagekit 中的 cachefile_name 属性。

这是我的代码:

class Thumb150x150(ImageSpec):
    processors = [ResizeToFill(150, 150)]
    format = 'JPEG'
    options = {'quality': 90}

    @property
    def cachefile_name(self):
        # simplified for this example
        return "bla/blub/test.jpg"

register.generator('blablub:thumb_150x150', Thumb150x150)

class Avatar(models.Model):
avatar= ProcessedImageField(upload_to=upload_to,
                            processors=[ConvertToRGBA()],
                            format='JPEG',
                            options={'quality': 60})
avatar_thumb = ImageSpecField(source='avatar',
                              id='blablub:thumb_150x150')

它根本不起作用。
当我调试时(没有覆盖cachefile_name),并查看cachefile_name的返回值,结果是一个字符串,如“CACHE/blablub/asdlkfjasd09fsaud0fj.jpg ”。我的错在哪里?

有什么想法吗?

【问题讨论】:

  • 你能用这个包含模型吗?
  • 我已经添加了我的模型
  • 您是否尝试过将 spec=Thumb150x150 传递给 ImageSpecField?
  • 没有改变,你有覆盖cachefile_name的例子吗? PS:当我在 cachefile_name 中返回我的字符串时,我得到了一个错误*** ValueError: need more than 1 value to unpack
  • 不幸的是,我没有。对不起。

标签: python django django-imagekit


【解决方案1】:

尽可能地复制该示例,它运行良好。几个建议是:

1) 确保您在视图中使用 avatar_thumb。直到那时才会生成文件“bla/blub/test.jpg”。

2) 检查你的 MEDIA_ROOT 的配置,确保你知道“bla/blub/test.jpg”应该出现在哪里。

让我举一个我正在做的类似事情的例子。我想为我的缩略图提供可以从原始文件名预测的唯一名称。 Imagekit 的默认方案基于哈希命名缩略图,我猜不出来。而不是这个:

media/12345.jpg
media/CACHE/12345/abcde.jpg

我想要这个:

media/photos/original/12345.jpg
media/photos/thumbs/12345.jpg

覆盖 IMAGEKIT_SPEC_CACHEFILE_NAMER 不起作用,因为我不希望所有缓存文件都位于“thumbs”目录中,而只是那些从特定模型中的特定字段生成的文件。

所以我创建了这个 ImageSpec 子类并注册了它:

class ThumbnailSpec(ImageSpec):
    processors=[Thumbnail(200, 200, Anchor.CENTER, crop=True, upscale=False)]
    format='JPEG'
    options={'quality': 90}

    # put thumbnails into the "photos/thumbs" folder and
    # name them the same as the source file
    @property
    def cachefile_name(self):
        source_filename = getattr(self.source, 'name', None)
        s = "photos/thumbs/" + source_filename
        return s

register.generator('myapp:thumbnail', ThumbnailSpec)

然后像这样在我的模型中使用它:

# provide a unique name for each image file
def get_file_path(instance, filename):
    ext = filename.split('.')[-1]
    return "%s.%s" % (uuid.uuid4(), ext.lower())

# store the original images in the 'photos/original' directory
photoStorage = FileSystemStorage(
    location=os.path.join(settings.MEDIA_ROOT, 'photos/original'),
    base_url='/photos/original')

class Photo(models.Model):
    image = models.ImageField(storage=photoStorage, upload_to=get_file_path)
    thumb = ImageSpecField(
        source='image',
        id='myapp:thumbnail')

【讨论】:

    【解决方案2】:

    我认为,正确的方法是设置IMAGEKIT_SPEC_CACHEFILE_NAMER。看看默认名称names.py,它将 settings.IMAGEKIT_CACHEFILE_DIR 与文件路径和哈希连接起来,你应该也这样做。

    【讨论】:

      猜你喜欢
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2014-05-12
      相关资源
      最近更新 更多