gpt4 book ai didi

django - 使用带有 sorl-thumbnail 的静态图像

转载 作者:行者123 更新时间:2023-12-04 18:13:52 26 4
gpt4 key购买 nike

我正在尝试提供驻留在我的 STATIC_ROOT 文件夹中的文件的缩略图。如果它最终出现在 MEDIA_URL/cache 中并不重要,但是 sorl-thumbnail 不会从静态文件夹加载图像。

当前代码:

{% thumbnail "images/store/no_image.png" "125x125" as thumb %}

有效的黑客
{% thumbnail "http://localhost/my_project/static/images/store/no_image.png" "125x125" as thumb %}

我不喜欢黑客,因为
A)它不干(我的项目实际上是从/的子目录提供的
B)它使用 http 来抓取一个只有 3 个目录的文件,似乎毫无意义地低效

最佳答案

我通过将文件从我的 View 传递到模板上下文来解决这个问题。

这是我从 View 中调用的示例 util 函数:

def get_placeholder_image():
from django.core.files.images import ImageFile
from django.core.files.storage import get_storage_class
storage_class = get_storage_class(settings.STATICFILES_STORAGE)
storage = storage_class()
placeholder = storage.open(settings.PLACEHOLDER_IMAGE_PATH)
image = ImageFile(placeholder)
image.storage = storage
return image

您可能可以执行类似于自定义模板标记的操作。

关于django - 使用带有 sorl-thumbnail 的静态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7490684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com