Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use sorl-thumbnail? (django)

I've been looking at the sorl-thumbnail's documentation, and I still can't figure out how to:

  1. upload images to sorl-thumbnail.
  2. selectively show images from sorl-thumbnail. (for example, load a specific image from sorl-thumbnail from a view and show it, with customized size, etc.)

Could you give some specific examples on how to use this library in a django view?

like image 315
user269334 Avatar asked Dec 01 '22 05:12

user269334


2 Answers

If you want greater flexibility you can generate the thumbnail right in the view. The following is straight from the sorl-thumbnail documentation:

from sorl.thumbnail import get_thumbnail

im = get_thumbnail(my_file, '100x100', crop='center', quality=99)

im then is the same thing as what you'd get back from the thumbnail template tag. So, you can add that variable to the template context, or just some part of it. For example if you just wanted the URL:

my_context = {
    'image_url': im.url,
}
like image 93
Chris Pratt Avatar answered Dec 06 '22 10:12

Chris Pratt


You can use sorl.thumbnail using the thumbnail template tags. Here's an example:

{% load thumbnail %}

{% thumbnail recipe.image "430x250" as thumb %}
     <img src="{{ thumb.url }}" width="{{ thumb.width }}" height="{{ thumb.height }}" alt="{{ recipe.title }}" />
{% endthumbnail %}

You don't upload images to sorl.thumbnail or load them from sorl.thumbnail. After proper configuration it will resize and store the images automatically and you can use the thumbnail template tag to get the proper URL of the image.

like image 24
Simeon Visser Avatar answered Dec 06 '22 10:12

Simeon Visser