0

I'm using thumbnail to display image previews. However, thumbnail is not able to create cache folder and thumbnail_kvstore in mysql database is always empty.

Here is my code: In settings.py:

INSTALLED_APPS = (
...
'sorl.thumbnail',
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/').replace('\\','/')
MEDIA_URL = '/media/'

In urls.py:

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

In models.py:

from sorl.thumbnail import ImageField

class Image(models.Model):
    file = models.ImageField(upload_to="images")
    user = models.ForeignKey(User)

In template:

{% thumbnail image "100x100" crop="center" as im %}
    <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}

The im.url generated by thumbnail looks like "/media/cache/6f/9d/6f9dc8d01bf2a23448525d0902a63cd6.jpg"

However, there is no cache folder generated under media/ and my images are stored in media/images. Also the thumbnail_kvstore table in mysql database is always empty.

I have seen several similar questions(Question1 or Question2). It seems that MEDIA_ROOT or MEDIA_URL is not properly configured. Could anyone help me here?

Community
  • 1
  • 1
Tao Huang
  • 1,181
  • 1
  • 9
  • 14

1 Answers1

0

It turns out I was using the wrong the ImageField. In models.py, I shoule use sorl.thumbnail.ImageField, instead of the built-in django.db.models.ImageField.

Tao Huang
  • 1,181
  • 1
  • 9
  • 14