我正在使用以下模板标签在我的模板中显示缩略图
{% load thumbnail %}
{% thumbnail obj.image 250x250 crop %}
缩略图模板标签按预期返回缩略图文件的相对 URL。但我希望它返回绝对网址。通常,easy-thumbnails 具有 THUMBNAIL_MEDIA_URL = '' 设置,它允许缩略图的存储构建绝对 url,但它不适用于 django-filer。
还有其他方法可以实现我想要的吗?
我正在使用以下模板标签在我的模板中显示缩略图
{% load thumbnail %}
{% thumbnail obj.image 250x250 crop %}
缩略图模板标签按预期返回缩略图文件的相对 URL。但我希望它返回绝对网址。通常,easy-thumbnails 具有 THUMBNAIL_MEDIA_URL = '' 设置,它允许缩略图的存储构建绝对 url,但它不适用于 django-filer。
还有其他方法可以实现我想要的吗?
您可以使用提取缩略图属性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