Search code examples
djangoeasy-thumbnailsdjango-filer

How to retrieve absolute urls to thumbnails generated via django-filer easy-thumbnails


I am using following template tags to display thumbnails in my templates

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

thumbnail template tag returns relative urls to thumbnail image files as expected. But I would like it to return absolute urls. Normally easy-thumbnails has THUMBNAIL_MEDIA_URL = '' setting which allows thumbnail's storage to build absolute urls but it does not work with django-filer.

Is there any other way to achieve what I want?


Solution

  • You can extract the thumbnail properties using as

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

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

    EDIT:

    If by "absolute" you mean including the website, I would recommend doing the logic elsewhere.

    Considering image to be a FilerImageField, create a property in the models.py. Example:

    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
    

    See https://github.com/SmileyChris/easy-thumbnails#manually-specifying-size--options