Sorl-thumbnail: 提供静态文件

创建于 2011-09-20  ·  3评论  ·  资料来源: jazzband/sorl-thumbnail

我找不到任何方法来缩略图静态(在 STATIC_ROOT 中)文件。 它似乎只适用于外部(http://....)和媒体(在 MEDIA_ROOT 中)文件。

当静态图像的输出大小并不总是相同(通过用户偏好、主题等)时,这非常有用

最有用的评论

这是一个补丁示例https://github.com/marcinn/sorl-thumbnail/commit/61557b41169f0656063c557735ef62908a5fcaab

编辑:对不起,似乎thumbnail.url的输出是有效的,但是从cached.url检索时,它的前缀是/media/而不是/static/ 。 里面还有更坏的东西。

编辑 (2) :反序列化存储代码存在设计问题 - 使用默认值而不是使用现有实例加载和实例化存储类。 换句话说 - sorl.thumbnail 不重用存储实例,而是创建自己的(配置不同)。 作为一种解决方法,我们可以从 FileSystemStorage 继承并定义自定义初始化程序。

应用它后,任何人都可以轻松地为任何东西生成缩略图。
可以创建一个像这样的自定义模板标签:

from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django import template
from sorl.thumbnail.templatetags.thumbnail import ThumbnailNode


register = template.Library()

class StaticThumbnailStorage(FileSystemStorage):
    def __init__(self, *args, **kw):
        super(StaticThumbnailStorage, self).__init__(
            *args, location='/path/to/custom/static/'
            base_url=settings.STATIC_URL, **kw)

storage = StaticThumbnaikStorage()

class StaticThumbnailNode(ThumbnailNode):
    def _get_thumbnail(self, file_, geometry, **options):
        options['storage'] = storage
        return super(StaticThumbnailNode, self)._get_thumbnail(
                file_, geometry, **options)


@register.tag
def static_thumbnail(parser, token):
    return StaticThumbnailNode(parser, token)

请注意,无需更改设置。

所有3条评论

您需要使用一些使用 STATIC_ROOT 的存储,例如您可以使用 'django.core.files.storage.FileSystemStorage' 并使用 location=settings.STATIC_ROOT 和 base_url=settings.STATIC_URL 进行实例化,请继续使用 stackoverflow,因为这不是漏洞。

五年后仍然不可能在不更改默认存储的情况下从静态文件生成拇指,因为https://github.com/mariocesar/sorl-thumbnail/blob/master/sorl/thumbnail/base.py#L100
由于实施,StackOverflow 将无济于事。

有机会解决这个问题吗? 通过get_thumbnail()传递自定义存储实例就足够了。

这是一个补丁示例https://github.com/marcinn/sorl-thumbnail/commit/61557b41169f0656063c557735ef62908a5fcaab

编辑:对不起,似乎thumbnail.url的输出是有效的,但是从cached.url检索时,它的前缀是/media/而不是/static/ 。 里面还有更坏的东西。

编辑 (2) :反序列化存储代码存在设计问题 - 使用默认值而不是使用现有实例加载和实例化存储类。 换句话说 - sorl.thumbnail 不重用存储实例,而是创建自己的(配置不同)。 作为一种解决方法,我们可以从 FileSystemStorage 继承并定义自定义初始化程序。

应用它后,任何人都可以轻松地为任何东西生成缩略图。
可以创建一个像这样的自定义模板标签:

from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django import template
from sorl.thumbnail.templatetags.thumbnail import ThumbnailNode


register = template.Library()

class StaticThumbnailStorage(FileSystemStorage):
    def __init__(self, *args, **kw):
        super(StaticThumbnailStorage, self).__init__(
            *args, location='/path/to/custom/static/'
            base_url=settings.STATIC_URL, **kw)

storage = StaticThumbnaikStorage()

class StaticThumbnailNode(ThumbnailNode):
    def _get_thumbnail(self, file_, geometry, **options):
        options['storage'] = storage
        return super(StaticThumbnailNode, self)._get_thumbnail(
                file_, geometry, **options)


@register.tag
def static_thumbnail(parser, token):
    return StaticThumbnailNode(parser, token)

请注意,无需更改设置。

此页面是否有帮助?
0 / 5 - 0 等级