Search code examples
djangothumbnailssorl-thumbnail

Thumbnail doesn't display with sorl thumbnail


I'm a newbie on Django. I'm trying to use Sorl thumbnail in a template. But i don't figure out why it's not working.

I can display image without using thumbnail tags (in my template). But when, i use them, it's not working.

Below, the code that i create:

My model:

from __future__ import unicode_literals

from django.db import models
from sorl.thumbnail import ImageField

def upload_location(instance, filename):
return "photos_portfolio/%s/%s/%s" %(instance.categorie, instance.slug, filename)

class Portfolio(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=160)
image = models.ImageField(upload_to=upload_location)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
categorie = models.ForeignKey('categorie.Categorie')
article = models.ForeignKey('posts.Post')   

def __unicode__(self):
    return self.title

def __str__(self):
        return self.title

class Meta:
    ordering = ["-timestamp"]

My View:

def portfolio(request):
afficher = Portfolio.objects.all()
return render(request, 'portfolio.html', {'afficher': afficher})  

My template:

     {% for photo in afficher %}  

     <div class="image-portfolio">

     <a class="example-image-link" href="{{ photo.image.url }}" data-lightbox="example-set" data-title="Click the right half of the image to move forward.">

     {% thumbnail photo.image "100x100" crop="center" as im %}
          <img class="example-image" src="{{ im.url }}" alt=""/>
     {% endthumbnail %}    

     </a>
     </div>

    {% empty %}

    <p>No image.</p>

  {% endfor %} 

Do you have any idea where my mistake is?

Thanks in advance for your help

Singertwist


Solution

  • No DB tables for sorl key-value storage.

    ./manage.py makemigrations thumbnail
    ./manage.py migrate