我正在尝试提供驻留在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个目录的文件,似乎效率很低
发布于 2011-09-21 03:46:06
假设您使用的是Django1.3,那么您应该看看关于Managing static files的文档
如果您正确设置了所有内容,则可以像这样包含您的图像:
<img src="{{ STATIC_URL }}images/store/no_image.png" />发布于 2012-04-13 11:59:38
我通过将一个文件从我的视图传递到模板上下文来解决这个问题。
下面是我从视图中调用的一个示例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您可能会做一些类似于自定义模板标记的事情。
发布于 2012-06-23 21:27:31
模板过滤器起作用了。但我不确定,是否每次都会从存储器中读取。如果是这样,这是不合理的.
from django.template import Library
from django.core.files.images import ImageFile
from django.core.files.storage import get_storage_class
register = Library()
@register.filter
def static_image(path):
"""
{% thumbnail "/img/default_avatar.png"|static_image "50x50" as img %}
<img src="{{ MEDIA_URL }}{{img}}"/>
{% endthumbnail %}
"""
storage_class = get_storage_class(settings.STATICFILES_STORAGE)
storage = storage_class()
image = ImageFile(storage.open(path))
image.storage = storage
return imagehttps://stackoverflow.com/questions/7490684
复制相似问题