[Solved]-How to thumbnail static files?

7👍

Ugly option that worked for me, passing in the path that you would normally pass to the static template tag (note that it assumes the http protocol, so it could be improved):

{% with 'http://'|add:request.get_host|add:STATIC_URL|add:image_path as path %}
    {% thumbnail path "720x306" crop="center" as im %}
      <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
    {% endthumbnail %}
{% endwith %}

This works by building up the absolute path to the static image.

3👍

Honestly…this looks fine; which means there is probably something simple wrong in your setup.

Possible bad setup:
How are you defining STATIC_URL in your settings? Also, what is the value of DEBUG (make sure this is set to True if you’re developing locally)? As @goliney pointed out, your path might be messed up. Try pulling out the thumbnail blocks out, and set the src of your image to {{ STATIC_URL }}/images/empty-news.jpg and verify that works before trying to do the thumbnails.

Forgot to load thumbnails: Make sure to put {% load thumbnail %} in your template before any references to the {% thumbnail %} block.

👤Dave

3👍

The following will work

{% with STATIC_URL|add:"/images/empty-news.jpg" as path %}
    {% thumbnail path "80x80" crop="center" as im %}
        <a href="#" class="image">
            <img alt="" src="{{im.url}}" class="frame2"></a>
    {% endthumbnail %}
{% endwith %}

2👍

I’m working through the same issue myself. It seems that if you want to use STATIC_URL in your templates you’ll need to make sure that path you pass to the thumbnail tag is absolute (treating the path as if it were external.)

Apparently the relative paths only work for images within the MEDIA_ROOT, seemingly designed for images coming from models.

As a test, try typing in the full http path.

See:
http://sorl-thumbnail.readthedocs.org/en/latest/examples.html

👤sframe

1👍

To cover a little more the uglyness i made a custom filter, using a constant in settings.py
SITE_URL:

settings.py

[...]
SITE_URL = "google.it"
[...]

templatetags/staticthumb.py

from django.conf import settings

from django import template

register = template.Library()

@register.filter()
def static_url(value):
    return ''.join(["http://", settings.SITE_URL, settings.STATIC_URL, value])

Then to use it in the template:

{% load thumbnail staticthumb %}

{% with image_path|static_url as path %}
   {% thumbnail path "720x306" crop="center" as im %}
      <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
   {% endthumbnail %}
{% endwith %}
👤Marcs

Leave a comment