Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to thumbnail static files?

I want to resize my static files with sorl thumbnail but it doesnt work

here is my codes

{% if not new.photo %}

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

{% else %}
{% thumbnail new.photo "80x80" crop="center" as im %}
<a href="{% url news_detail new.slug %}" class="image">
<img alt="" src="{{im.url}}" class="frame2"></a>
{% endthumbnail %}
{% endif %}

If I have image it shows image but when I dont have image I cant use default image because thumbnail doesn't work

like image 677
aysekucuk Avatar asked Oct 18 '12 14:10

aysekucuk


2 Answers

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.

like image 169
seddonym Avatar answered Nov 15 '22 22:11

seddonym


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.

like image 33
Dave Avatar answered Nov 15 '22 21:11

Dave