skip to Main Content

I am trying to use sorl-thumbnail in my templates but I have been unable to successfully generate the thumbnails in the proper ‘/cache’ directory that ‘sorl-thumbnail’ uses to retrieve thumbnails.

It seems that this problem has happened to a number of people before, however previous solutions have not worked for me. I have run ./manage.py migrate and I have ensured that I am using the sorl.thumbnail.ImageField rather than the default django.db.models.ImageField in my models.py and I have tried using ./manage.py thumbnail cleanup and ./manage.py thumbnail clear. I have also read that memcached could be interfering with things, but even with all mentions of memcached commented out, I can’t get sorl-thumbnails to work.

For reference, I am running my project on an Ubuntu 18.04.2 apache2 server hosted by DigitalOcean.

html:

{% for obj in object_list %}
    ...
    {% thumbnail obj.image "75x75" as im %}
        <img class = "artwork" src="{{im.url }}"/>
    {% endthumbnail %}
    ...
{% endfor %}

settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'sorl.thumbnail',
    'posts',
    'login',
]

...

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "/home/arthouse/workspace/code/side_proj/assets/")
]

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "/home/arthouse/workspace/code/side_proj/static_cdn/")

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "/media_cdn")

models.py:

from sorl.thumbnail import ImageField

class Post(models.Model):
    image = ImageField(upload_to=upload_location,
        null=True,
        blank=True,
        width_field="width_field",
        height_field="height_field")

I have set THUMBNAIL_DEBUG=True but no output comes from this. I can see that thumbnails are generated, (eg: media/cache/6d/13/6d13206b5207bf2b6234295b749f1419.jpg) but there is never a /cache directory created within my MEDIA_ROOT which leads to a 404 error.

I’m happy to share more code snippets if that would help!

2

Answers


  1. Chosen as BEST ANSWER

    As suspected, Sorl Thumbnail was working without issue and the problem was user error. I had incorrectly configured MEDIA_ROOT in my settings.py which led to Sorl Thumbnail trying to retrieve media from a non-existent location.


  2. Try setting the Solr Thumbnail setting that defines where it uploads files:

    THUMBNAIL_PREFIX = ‘CACHE/’

    Then run collectstatic –noinput as usual. In Digital Ocean you should see a new folder called CACHE created in your bucket.

    The 404 error sounds like you’ve forgotten the following two settings:

    AWS_S3_ENDPOINT_URL – Defines how the static tag rewrites the img url to point to its location in your bucket.

    AWS_S3_CUSTOM_DOMAIN – Same as above if you use a custom domain.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search