我有一個我想在django模板中顯示的值列表。Django:在模板中創建一個變量
列表是或多或少像這樣:
199801 string1
199802 string2
199904 string3
200003 string4
200011 string5
其中第一列是在形式YYYYMM日期和第二個是一個通用的字符串
列表是按日期降序
有序我想創建是今年 這樣的分組字符串X的列表:
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文檔:*模板系統是爲了表達表示,而不是程序邏輯。* – mykhal 2010-07-06 22:06:56