如何获取通过Django-filer easy-thumbnails生成的缩略图的绝对URL?

3
我正在使用以下模板标签在我的模板中显示缩略图。
{% load thumbnail %}
{% thumbnail obj.image 250x250 crop %}

缩略图模板标签按预期返回缩略图图像文件的相对URL。但我希望它返回绝对URL。通常,easy-thumbnails具有THUMBNAIL_MEDIA_URL =''设置,允许缩略图存储构建绝对URL,但它与django-filer不兼容。

是否有其他方法可以实现我想要的效果?

1个回答

1
您可以使用as提取缩略图属性。
{% thumbnail obj.image 250x250 as thumb %}
{{thumb.url}}

查看http://easy-thumbnails.readthedocs.org/en/latest/usage/#thumbnail-tag

编辑:

如果您所谓的“绝对”是包括网站,我建议在其他地方执行逻辑。

image视为FilerImageField,在models.py中创建一个属性。例如:

from django.contrib.sites.models import Site
from easy_thumbnails.files import get_thumbnailer

@property
def thumbnail_absolute_url(self):
    if self.image:
        thumbnailer_options = {'size': (250, 250), 'crop': True}
        thumb = get_thumbnailer(self.image).get_thumbnail(thumbnailer_options)
        thumb_url = thumb.url
        site = Site.objects.get_current()
        return site.domain + thumb_url
    return None

请查看https://github.com/SmileyChris/easy-thumbnails#manually-specifying-size--options

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接