0
votes

I have trouble displaying the thumbnail image generated by Sorl-thumbnail. Here is my view.py:

def index(request):
    if request.method == 'GET':
        im = get_thumbnail('/Users/cheng/Dev/notes_proj/images/2015/4/8/c0eb6152bcb74c31c6eff3562513ee6507f8657d.png', '100x100')
        context = {'im': im}
        return render(request, 'index.html', context)

Here is my template:

{% load thumbnail %}
    {% thumbnail im as img %}
    <img src="{{ img.url }}">
{% endthumbnail %}

Here is the error message:

[09/Apr/2015 16:27:35] "GET /cache/51/03/5103e40af16185770a5928d7f5a5b31a.jpg HTTP/1.1" 404 13744

The THUMBNAIL_DEBUG = True is turned on and there is no error message from Sorl.

I am using the default database cache approach (which means no extra setup required besides adding sorl to INSTALLED_APP and run "python manage.py migrate")

I have searched my hard drive and there is no "/cache/51/03/5103e40af16185770a5928d7f5a5b31a.jpg"

My current setup does not allow me to use 'ImageField' in my model. So I simply stored the absolute path to the image file and used the low level API of sorl to generate the thumbail as shown above.

I saw this post: Sorl-thumbnail bad url's
I added the MEDIA_URL and MEDIA_ROOT to my settings.py:

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

Still getting the 404 error:

09/Apr/2015 16:45:43] "GET /images/cache/51/03/5103e40af16185770a5928d7f5a5b31a.jpg HTTP/1.1" 404 13767

I checked the database table 'thumbnail_kvstore'. It is always empty.

I am using:

sorl-thumbnail (12.2) Django (1.7.7)

So which part did I do wrong?

Thanks a lot!

1
try clear and cleanup ...Raja Simon
@RajaSimon Thanks for the suggestions, I did that and it still does not work. It seems to me that Sorl did not generate the thumbnail image at all for some reason. The database cache shouldn't be empty right?Cheng
@RajaSimon I went into the folder and found the image file I referenced is 0 byes for some reason.. After I restored the image, I can see the cache being created in the database and I found the cache folder in the media root also I can see the thumbnail. BUT, when I loaded it in my template, it still gives me a 404 even though the thumbnail file is there... I will keep diggingCheng
keep going you will get it ..Raja Simon

1 Answers

1
votes

I would like to document what happened and provide some suggestions for people who are new to sorl like me:

  1. Check the database cache or whatever caching backends you are using to see if there is anything under the table named thumbnail_kvstore. For what I have encountered, the original image file that I tried to generate a thumbnail from was 0 byte. Sorl did generate a key for the thumbnail even though the file is empty. However, the key itself is not stored in the database. I guess sorl would only update the database table if an actual thumbnail file has been created on the hard drive.

  2. Check your hard drive. Once the thumbnail file is created, you can print out the url:

    im = get_thumbnail('/Users/cheng/Dev/notes_proj/images/2015/4/8/c0eb6152bcb74c31c6eff3562513ee6507f8657d.png')
    print im.url
    

This is the print out I saw:

images/cache/e6/99/e699913d4ee776453e5c37108decb1bc.jpg

Now, go to your MEDIA_ROOT, and see if there is a new directory named cache, and see if the .jpg thumbnail is in there.

  1. If you can verify that the thumbnail file is there but still getting a 404. Now go to check your urls.py, make sure you have something like this:

    from django.conf import settings
    
    urlpatterns = patterns('',
            ...
            url(r'^images/(.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
    )
    

Assuming you have set the MEDIA_URL as the following:

MEDIA_ROOT ='images/'

Adjust the name to suit your needs.

Everything should work now.

P.S. One more thing to note is that in production, you might have another server to server static files. In that case, you should remove:

url(r'^images/(.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),

from your urls.py file.