问题描述
我一直在观看sorl-thumbnail的文档,我仍然无法弄清楚如何:
1.将图像上传到sorl-thumbnail。
2.有选择地显示来自sorl-thumbnail的图像。
(例如,从视图加载来自sorl-thumbnail的特定图像,并显示,具有自定义尺寸等)。
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.)
你能给出一些具体的关于如何在django视图中使用此库的示例?
Could you give some specific examples on how to use this library in a django view?
提前感谢:)
推荐答案
如果您想要更大的灵活性,您可以在视图中直接生成缩略图。以下是:
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
然后与您从
缩略图
模板标签中返回的内容是一样的。因此,您可以将该变量添加到模板上下文中,或者只是其中的一部分。例如,如果您只想要URL:
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,
}
这篇关于如何使用sorl-thumbnail? (django的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!