【问题标题】:Django: Images inside conditional template tagsDjango:条件模板标签内的图像
【发布时间】: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


【解决方案1】:

啊..我想通了。

我假设 {% load static %} 将从它扩展的基本模板中获取。不是,因此将 {% load static %} 添加到子模板中清除了它。

【讨论】:

    猜你喜欢
    • 2013-04-23
    • 2014-07-25
    • 2014-04-03
    • 2018-11-19
    • 2011-09-30
    • 2016-01-13
    • 2011-12-11
    • 1970-01-01
    • 2020-09-27
    相关资源
    最近更新 更多