Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django sorl-thumbnail does not display images

Tags:

django

I try to use Django sorl-thumbnail but it does not display images (and does't show any errors).

Settings.py:

INSTALLED_APPS = (
    ....
    'sorl.thumbnail',
)

Models:

class Toy(models.Model):
    name = models.CharField(max_length=50, verbose_name=u'Name')
    image = ImageField(upload_to='site_media/images')

templates:

<div id="toy">
                {% for p in toys %}
                    <div class="toy">
                                # toy.image - this is model_name.image field
                            {% thumbnail toy.image "100x700" as im %}
                            <img style="margin:{{ im|margin:"100x700" }}" src="{{ im.url }}" width="{{ im.x }}" height="{{ im.y }}">
                            {% endthumbnail %}

                            <p>
                            <span> {{ p.name }} </span>
                            <span> {{p.unit_price}} </span>
                            </p>
                    </div>
                {% endfor %}
            </div>

urls.py:

url(r'^site_media/(.*)$', 'django.views.static.serve', {'document_root' : os.path.join(os.path.dirname(__file__), 'site_media')}),
like image 383
euro98 Avatar asked Oct 07 '12 08:10

euro98


2 Answers

Actually, using the ImageField from sorl is only necessary to automatically delete thumbnails when the original image is deleted.

sorl.thumbnail by default does not display any errors and fails silently if thumbnail creation was not successful. To see the errors, add

THUMBNAIL_DEBUG = True

to your settings.py. This should help you fix the problem.

like image 109
sjaensch Avatar answered Sep 20 '22 05:09

sjaensch


Have you imported ImageField from sorl in model definition? It's necessary to get it working automatically.

Moreover you need to run in console ./manage.py syncdb because in default configuration sorl keeps cached names of thumbnails in database. It has to create its own table in database for that.

Could you also show your settings of STATIC_URL, STATICFILES_DIRS, etc?

like image 38
Przemek Lewandowski Avatar answered Sep 21 '22 05:09

Przemek Lewandowski