【发布时间】:2018-08-14 14:25:43
【问题描述】:
我正在尝试根据 Django 模板中的某些条件显示图像,但 Django 似乎不喜欢在条件中使用静态标记。
代码如下:
<td>
{% if result.threshold == "normal" %}
<img src="{% static "face-green-small.jpg" %}" alt="Green"/>
{% endif %}
{% if result.threshold == "high" or result.threshold == "low" %}
<img src="{% static "face-amber-small.jpg" %}" alt="Amber"/>
{% endif %}
{% if result.thresdholf == "vhigh" or result.threshold == "vlow" %}
<img src="{% static "face-red-small.jpg" %}" alt="Red"/>
{% endif %}">
</td>
我得到的错误是:
第 32 行的块标记无效:'static'、预期的 'elif'、'else' 或 'endif'。您是否忘记注册或加载此标签?
我确定 static 已注册,因为它已在模板中使用过。
任何关于我做错了什么的指导将不胜感激。
【问题讨论】:
-
请显示模板的其余部分,包括加载和使用静态的位置。
-
您需要在模板顶部{% load static %}
标签: django templates static conditional