alj*_*alj 3 django django-templates
我有一个"促销"事件的查询集,它在模板中呈现.这些促销活动中的每一个也有一个或多个约会.我想要做的是显示第一次和最后一次约会的日期.
到目前为止,使用"第一个"标签工作.但是,使用"last"标记会导致:
TemplateSyntaxError异常值:呈现时捕获异常:不支持负索引.
这是模板脚本
{% for promotion in promotions%}
{% with promotion.appointment_set.all as appointments %}
{% with appointments|first as first_ap %}
{{ first_ap.date|date }}
{% endwith %}
{% with appointments|last as last_ap %}
{{ last_ap.date|date }}
{% endwith %}
{% endwith %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
在将查询集提供给模板之前将其转换为列表也可以获得您想要的位置:
return render_to_response(template, {
appointments: list(Appointments.objects.all())
})
Run Code Online (Sandbox Code Playgroud)
由于我正在使用整个列表,我做了类似的事情(可能会有改进):
{% for ap in appointments %}
{% ifequal ap appointments|last %}
ap.date
{% endifequal %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
对象属性仍然有效.例如:ap.user.full_name
| 归档时间: |
|
| 查看次数: |
1986 次 |
| 最近记录: |