我在python中有一个list of dictionary
即
listofobs = [{'timestamp': datetime.datetime(2012, 7, 6, 12, 39, 52), 'ip': u'1.4.128.0', 'user': u'lovestone'}, {'timestamp': datetime.datetime(2012, 7, 6, 12, 40, 32), 'ip': u'192.168.21.45', 'user': u'b'}]
我想使用Django模板中listofobs
变量的所有键和值。例如:
对于第一次迭代:
timestamp = 7 july 2012, 12:39 Am
ip = 1.4.128.0
user = lovestone
对于第二次迭代:
timestamp = 7 july 2012, 12:40 Am
ip = 192.168.21.45
user = b
等等。。
将遍历dict的列表,然后在模板中使用它们,只需将其包装为所需的django语法,这与常规python非常相似。
Django模板语法允许您循环遍历dict列表:
您只需要确保listofobjs在您的上下文中进行呈现。
看看built in ^{} template tag 的例子。
在项目上循环(外部循环):
然后循环遍历dictionary对象中的项:
相关问题 更多 >
编程相关推荐