10

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')}),
euro98
  • 127
  • 1
  • 7

2 Answers2

15

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.

sjaensch
  • 907
  • 6
  • 9
3

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?