0

From the moment I defined my MEDIA_ROOT in my settings.py I could not get sorl-thumbnail to generate the cache folder with images. Before that, I did not set MEDIA_ROOT in and it was working just fine.

Please note that I am in test with DEBUG=True.

Here are my settings:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DEBUG = True
TEMPLATE_DEBUG = DEBUG

INSTALLED_APPS = (
    'django.contrib.sites',
    'grappelli',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'djangobower',
    'pipeline',
    'sorl.thumbnail',
    'django_countries',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    # Local apps
    ...

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

THUMBNAIL_DEBUG = DEBUG
THUMBNAIL_PREFIX = 'cache/'

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'pipeline.finders.PipelineFinder',
)

# TODO : use redis in prod
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'cache',
    }
}

My home.html file contains this:

{% extends 'base.html' %}

{% load static i18n thumbnail %}

{% static 'main/img/business_man_in_the_mirror.jpg' as business_man_in_the_mirror %}
{{ business_man_in_the_mirror }}
{% thumbnail business_man_in_the_mirror|slice:"1:" "x300" as im %}
    {{ im.url }}
    <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"
         alt="{% trans "Homme d'affaire se regardant dans le miroir" %}"
         class="center-block img-responsive">
{% endthumbnail %}

{{ business_man_in_the_mirror }} prints "/static/main/img/business_man_in_the_mirror.jpg".

{{ im.url }} prints "/media/cache/d0/38/d038a3e64b2c7d070088e368ee881027.jpg"

So far so good. But the cache folder in MEDIA_ROOT never gets created. The MEDIA_ROOT even has 777. I do not see any error from ./manage.py runserver -v 3. So the image is missing in my web page.

The runserver log shows the following:

[10/Aug/2015 18:47:47]"GET /media/cache/d0/38/d038a3e64b2c7d070088e368ee881027.jpg HTTP/1.1" 404 2870

http://localhost:8000/static/main/img/business_man_in_the_mirror.jpg does show me the image.

I tried ./manage.py thumbnail cleanup and ./manage.py thumbnail clear but it had no effect. I also tried to delete the kvstore table and the cache table in DB, changed nothing. I have 5 rows in the cache but nothing in the kvstore table.

What am I missing here?

Q Caron
  • 952
  • 13
  • 26

1 Answers1

0

Please have a look to this question: How to thumbnail static files?. I did not see it at first but it gives the answer to my problem.

So if you do the same than me, e.g. thumbnailing static images without MEDIA_ROOT set and then set MEDIA_ROOT, this is why it does not work anymore.

Serving static files with sorl-thumbnail will not work with abspath to your static images. Instead, use the STATIC_URL in your template as a prefix by using something like http://localhost:8000 or www.example.com.

I do not know the impacts of such a practice in a production environment but it created a cache folder on the root of my Django project in local, which does not make me happy for the moment. I chose not to use thumbnail for my static image for the moment.

Community
  • 1
  • 1
Q Caron
  • 952
  • 13
  • 26