2017-05-24 55 views
0

现在,我有鉴于一个字典,并希望存在于HTML表格的形式: enter image description here如何遍历包含Django模板中的许多字典的列表?

和我通过它的来龙去脉:

context['cities']=cities 

然后,使其:

render(request,'index.html',context=context) 

在 '的index.html':

<table> 
    <tr> 
     <th>name</th> 
     <th>population</th> 
     <th>country</th> 
    </tr> 
    {% for city in cities%} 
    <tr> 
     <td>{{city.name}}</td> 
     <td>{{city.population}}</td> 
     <td>{{city.country}}</td> 
    </tr> 
    {% endfor %} 
</table> 

不幸的是,似乎Django模板不能支持这种遍历,所以结果不能像我期望的那样成功显示。 “td”标签为空。

+0

它应该工作,你能分享你的'views.py'吗? –

回答

0

做了尝试类似的东西:

render(request,'index.html',context={ 
    "cities": cities 
}) 

此外,您还可以尝试到您的模板:

{% for city in cities %} 
    <tr> 
     {% for key, value in city %} 
     <td>{{ value }}</td> 
     {% endfor %} 
    </tr> 
    {% endfor %} 
</table> 

keyname,​​等

相关问题