djangodjango-cmsdjango-filereasy-thumbnails

Accessing custom thumbnail options in templates


In a Django CMS project with Filer enabled, the Admin dashboard for Filer allows you to create custom Thumbnail Options, similar to defining an alias using easy-thumbnails.

How do I access these options in templates?

{{ obj.image.thumbnails }} allows me to choose from one of the DEFAULT_THUMBNAILS defined in abstract.py in the Filer package, such as admin_tiny_icon, but I can't for the life of me figure out how to access these custom options defined in the front-end.

Thumbnail images are also not automatically created in {{ MEDIA_ROOT }} for these custom Thumbnail Options when adding new images like they are for the DEFAULT_THUMBNAILS.


Solution

  • You could create your own templatetag to get the desired thumbnail option, something like this:

    // utils_tags.py
    
    from django.template import Library
    
    from filer.models import ThumbnailOption
    
    register = Library()
    
    
    @register.simple_tag
    def get_thumbnail_option(name, filerimage):
        option = ThumbnailOption.objects.filter(name=name).first()
        if option:
            thumbnailer = filerimage.easy_thumbnails_thumbnailer
            return thumbnailer.get_thumbnail(option.as_dict)
        return filerimage
    

    Then you could use it like this in a template:

    {% load utils_tags %}
    
    {% get_thumbnail_option "Custom thumbnail name" obj.image as thumb %}