如何在循环外访问列表中某个实例的属性?
<table>
{% for field in fields %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endfor %}
The value of the last field is: {{ WHAT_GOES_HERE??? }}
</table>
WHAT_GOES_HERE应该是什么?我已尝试过字段| last.value,fields.-1.value等,但不知道正确的语法。我想使用该值动态创建一个链接(参数指向下一组值的开头)
答案 0 :(得分:4)
Django仅针对这种情况提供了forloop object:
<table>
{% for field in fields %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% if forloop.last %}
The value of the last field is: {{ field.value }}
{% endif %}
{% endfor %}
</table>