Django: sorl-thumbnail
-
12-03-2021 - |
Question
This is my code
{% thumbnail ham.thumbnail_url "50x20" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
and this is what the img src output looks like
cache/81/a8/81a884b2bce95dfe60e7397ea15d10f2.jpg
even with this example from the docs
{% thumbnail "http://www.aino.se/media/i/logo.png" "40x40" crop="80% top" as im %}
<img src="{{ im.url }}">
{% endthumbnail %}
What's wrong?
Update:
{% thumbnail ham.thumbnail_url "220x120" crop="center" as foo %}
<img src="{{MEDIA_ROOT}}{{ foo.url }}" width="{{ foo.width }}" height="{{ foo.height }}">
{% endthumbnail %}
Now i have a directory called 'cache' inside the 'media' directory that contains all the images, and the generated code is
<img width="220" height="120" src="/media/cache/c6/5d/c65d3cd46c4ef6154c788526bfa3447f.jpg">
and this is my settings
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
but i can't see the images on screen == failed to load a given url(firebug)
One possible way to make it work is to auto generate the cache directory inside the static directory. Works for me if i copied the cache dir to static dir and changed the settings to
MEDIA_URL = ''
<img src="{{STATIC_URL}}{{ foo.url }}" width="{{ foo.width }}" height="{{ foo.height }}">
Solution
This is the standard way for sorl-thumbnail to name its files. The files are resized by whatever arguments you call thumbnail tag with. The result is a url which looks like cache/81/a8/81a884b2bce95dfe60e7397ea15d10f2.jpg. If you're not seeing anything you may want to look at your STATIC_URL settings.
Many sites have a static url, such that your image src will show up as
http://static.example.com/cache/81/a8/81a884b2bce95dfe60e7397ea15d10f2.jpg
or
http://example.com/media/cache/81/a8/81a884b2bce95dfe60e7397ea15d10f2.jpg
OTHER TIPS
Change your media root: MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
To something like this on LOCAL:
MEDIA_ROOT = /home/user/project/app/media/
MEDIA_URL = /site_media/
For production I did something like this:
MEDIA_ROOT = '/home/user/webapps/media/'
MEDIA_URL = 'http://domain.com/site_media/'
I have had a problem with this when I switched to the new sorl, but everything works for me now. Hope this helps.