Search code examples
djangomemcachedsorl-thumbnail

Sorl-thumbnail only working with DEBUG=True


I'm using Python 3.6.9, Django 3.2.8, sorl-thumbnail 12.7.0 with memcached and apache2

my thumbnails completely disappear and give a 404 when settings.py variable DEBUG is set to False.

I also have to say, that I don't get any errors displayed at all from setting THUMBNAIL_DEBUG = True as I understand it depends on the aforementioned variable which I have to set to False for pictures to disappear, if it does not depend on it I'm not getting any error displayed either.

I'm just loading them inside templates like this:


{% load thumbnail %}

{% thumbnail ann.obj.fotografia_professionista "460x310" as thumb %}<img src="{{ thumb.url }}" data-src="{{ thumb.url }}" class="img-fluid lazy">{% endthumbnail %}

and it works fine as long as DEBUG = True.

I restarted apache2 and memcached, I also used python manage.py thumbnail clear or cleanup but it just doesn't work at all.

I'm delivering the thumbnails at SETTINGS.MEDIA_URL, which is just set to "/media" and that I'm enabling in urls.py like this:

urlpatterns = [
 # my paths
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I did read this thread and I set up my urlpatterns the same exact way as it was suggested, but it doesn't solve my problem.

From reading this other thread, I also imagined that the combination between the variable DEBUG and THUMBNAIL_DEBUG could matter, but it seems it doesn't.

I'm supposing memcached is not responsible for this as it works alright with DEBUG = True, which means it's enabled and up and running as far as Django is concerned, so I'm suspecting a bug with either Django or sorl-thumbnail, or maybe apache2 configuration itself.


Solution

  • The problem was in my apache2 site configuration file that wasn't serving the /media Alias as the directory path had changed.

    Refer to this page of the documentation in case.

    Alias /robots.txt /path/to/mysite.com/static/robots.txt
    Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
    
    Alias /media/ /path/to/mysite.com/media/
    Alias /static/ /path/to/mysite.com/static/
    
    <Directory /path/to/mysite.com/static>
    Require all granted
    </Directory>
    
    <Directory /path/to/mysite.com/media>
    Require all granted
    </Directory>
    
    WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
    
    <Directory /path/to/mysite.com/mysite>
    <Files wsgi.py>
    Require all granted
    </Files>
    </Directory>