2017-05-24 59 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,​​等

相關問題