[Answered ]-Using easy-thumbnails (or other 3rd party library) with jinja2 in Django

2👍

You’ll need to add the filter to your Jinja2 environment:

def environment(**options):
    env = Environment(**options)
    env.globals.update(**{
        'static': staticfiles_storage.url,
        'url': reverse,
    })

    # add easy-thumbnails function as a Jinja2 filter
    from easy_thumbnails.templatetags.thumbnail import thumbnail_url
    env.filters.update(**{
        'thumbnail_url': thumbnail_url,
    })

    return env

You should be aware that the template tags in easy-thumbnails are built for Django templates. However, in this very specific case, the thumbnail_url function just also happens to work with Jinja2 templates.

A better implementation would be to write your own functions to wrap the functionality implemented in easy-thumbnails, and to use those functions as your Jinja2 filters instead.

Leave a comment