【问题标题】:How to retrieve absolute urls to thumbnails generated via django-filer easy-thumbnails如何检索通过 django-filer easy-thumbnails 生成的缩略图的绝对 URL
【发布时间】:2015-06-26 11:54:36
【问题描述】:

我正在使用以下模板标签在我的模板中显示缩略图

{% load thumbnail %}
{% thumbnail obj.image 250x250 crop %}

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

还有其他方法可以实现我想要的吗?

【问题讨论】:

    标签: django easy-thumbnails django-filer


    【解决方案1】:

    您可以使用as提取缩略图属性

    {% thumbnail obj.image 250x250 as thumb %}
    {{thumb.url}}
    

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

    编辑:

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

    考虑到imageFilerImageField,在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

    【讨论】:

      猜你喜欢
      • 2013-04-19
      • 2013-12-22
      • 2016-09-24
      • 2012-02-13
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      相关资源
      最近更新 更多