euro98
euro98

Reputation: 127

Django sorl-thumbnail does not display images

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')}),

Upvotes: 10

Views: 3904

Answers (2)

sjaensch
sjaensch

Reputation: 971

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.

Upvotes: 15

Przemek Lewandowski
Przemek Lewandowski

Reputation: 516

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?

Upvotes: 3

Related Questions