【发布时间】:2011-03-12 13:19:08
【问题描述】:
我有一个想要在 django 模板中显示的值列表。
列表或多或少是这样的:
199801 string1
199802 string2
199904 string3
200003 string4
200011 string5
第一列是格式为 YYYYMM 的日期,第二列是通用字符串
列表按日期顺序排列
我要创建的是按年份分组的 stringx 列表 像这样:
1998 string1, string2
1999 string3
2000 string4, string5
我查看了文档,我认为我唯一需要的是创建一个变量来存储我打印的“去年”,所以我可以执行以下操作:
if current_value.year != last_year
#create a new row with the new year and the string
else
#append the current string to the previous one
我认为我找到的唯一方法是编写一个自定义模板标签并让它存储变量和值...但是在开始编写代码之前我想知道是否有更简单的方法!
【问题讨论】:
-
你应该这样做。来自 Django 文档:模板系统旨在表达演示,而不是程序逻辑。