简体   繁体   中英

Sorl-thumbnail how to display thumbnail

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!

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.

PS 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM