【发布时间】:2013-08-24 05:53:18
【问题描述】:
我制作了这个自定义过滤器来检查图像是否存在:
from django import template
from django.core.files.storage import default_storage
register = template.Library()
@register.filter(name='file_exists')
def file_exists(filepath):
if default_storage.exists(filepath):
return filepath
else:
index = filepath.rfind('/')
new_filepath = filepath[:index] + '/image.png'
return new_filepath
我在模板中这样使用它:
<img src="{{ STATIC_URL }}images/{{ book.imageurl }}|file_exists" alt="{{book.title}} Cover Photo">
但它不起作用。我也不知道为什么。
【问题讨论】:
-
“不起作用”是什么意思?它返回什么?
-
我在图片的网址中得到
http://localhost:8000/static/images/ios.png|file_exists
标签: django django-models django-templates django-template-filters