下面是我用数据库中的数据创建的字典的打印结果。我试图在 html 中以日期顺序升序显示它。但是当我在模板中循环时,顺序不正确?我搜索并尝试了ordereddict,创建了一个自定义过滤器-->
@register.filter(name='sort')
def listsort(value):
if isinstance(value, dict):
new_dict = SortedDict()
key_list = value.keys()
key_list.sort()
for key in key_list:
new_dict[key] = value[key]
return new_dict
elif isinstance(value, list):
new_list = list(value)
new_list.sort()
return new_list
else:
return value
listsort.is_safe = True
没运气 !以下是我拥有的字典:
{u'Berplatform': {'days': {datetime.date(2012, 10, 31): {'': None},
datetime.date(2012, 11, 9): {<Event: Opening Coctail>: <Event: Opening Coctail>,
'': None},
datetime.date(2012, 11, 10): {'': None},
datetime.date(2012, 11, 11): {'': None},
datetime.date(2012, 11, 13): {'': None},
datetime.date(2012, 11, 17): {'': None},
datetime.date(2012, 11, 18): {'': None},
datetime.date(2012, 11, 24): {<Event: Platform is in Art İstanbul>: <Event: amberPlatform is in Art İstanbul>,
'': None},
datetime.date(2013, 7, 2): {<Event: ber12 exhibition 2>: <Event: amber12 exhibition 2>,
'': None}}},