【发布时间】:2020-05-06 17:43:31
【问题描述】:
我正在尝试使用 FOR LOOP 内的 IF 结构中的列表中的对象,但是当我尝试将对象与字符串(即“TRUE”)进行比较时,我无法进入 True case 行IF结构。
示例: 当equipo.Department =“Equipo”时,我不知道为什么IF({% ifequipo.Department == 'Equipo' %})不起作用。
代码:
{% autoescape off %}
{% if equipos %}
{% for equipo in equipos %}
<tr></tr>
<td>{% if equipo.Department == 'Equipo' %}
E
{% else %}{{ equipo.Department }}{% endif %}-{{ equipo.Equipment_id }}</td>
<td>{{ equipo.Name }}</td>
<td>{{ equipo.Description }}</td>
<td>{{ equipo.SerialNo }}</td>
<td>{{ equipo.Vendor }}</td>
<td>{{ equipo.Tag }}</td>
<td>{{ equipo.OutOfService }}</td>
<td>{{ equipo.Location }}</td>
<td>{{ equipo.Plan }}</td>
<td>{{ equipo.ManualPath }}</td>
<td>{{ equipo.ImagePath }}</td>
</tr>
{% endfor %}
{% else %}
<h1>No existen registros</h1>
{% endif %}
{% endautoescape %}
【问题讨论】:
-
Department可能不是CharField,而是ForeignKey? -
可以分享一下相关模型吗?
-
是否有可能更响应。
-
Willem,你的没错系来自ForeingKey:
-
Department = models.ForeignKey(Departamento, null=True, blank=True, on_delete=models.CASCADE)
标签: django for-loop if-statement templates