Get image thumbnail from easy-thumbnails by image location

1.9k views Asked by At

I have an icon image in all apps:

/app_name/static/model_name/images/icon.png

which is adding to model list in admin interface. And I want to crop it using easy-thumbnails and custom template tags:

template.html:

{% load project_tags %}
<th scope="row">{% load_icon model %}<a href="{{ model.admin_url }}">{{ model.name }}</a></th>

project_tags.py

from easy_thumbnails.files import get_thumbnailer
class LoadIcon(template.Node):

    def __init__(self, model_obj):
        self.model_obj = Variable(model_obj)

    def render(self, context):
        return '<img src="%s" />' % get_thumbnailer('/static/'+model_name+'/images/icon.png')['model_icon'].url

settings.py

THUMBNAIL_ALIASES = {
    '': {
        'model_icon': {'size': (20, 20), 'crop': True},
    },
}

But I have an "SuspiciousOperation" error: SuspiciousOperation at /admin/ Attempted access to '/polls/images/icon.png' denied.

Did I do something wrong ?

2

There are 2 answers

0
Philipp Zedler On

The path '/polls/images/icon.png' is indeed suspicious, but I cannot see in your code where it comes from. It is probably interpreted as an absolute path on your file system. Check where you construct this path and let it begin with something like settings.MEDIA_ROOT.

0
Lee On

Easy thumbnail uses a Django storage backend which is restricted to the MEDIA directory. The best way to resolve the problem would be to write a custom storage backend.