【发布时间】:2019-10-22 02:56:04
【问题描述】:
我的字典是这样的:
a = {
"1": {
"league_id": "1",
"name": "2018 Russia World Cup",
"country": "World",
"country_code": "",
"season": "2018",
"season_start": "2018-06-14",
"season_end": "2018-07-15",
"standings": False,
},
"2": {
"league_id": "2",
"name": "Premier League",
"country": "England",
"country_code": "GB",
"season": "2018",
"season_start": "2018-08-10",
"season_end": "2019-05-12",
"standings": True
},
}
我想遍历 dict 并显示“name”和“country”的所有值。
我的 .html 模板中的代码如下所示:
{% for c in a %}
<thead>{{ a[c]['country'] }}</thead>
{% endfor %}
{% for n in a %}
<tr>
<td>{{ a[n]['name'] }}</td>
</tr>
{% endfor %}
这给出了一个错误:
无法解析余数:'[c]['country']' from '联赛[c]['国家']'
我也试过
{% for c in leagues %}
<thead>{{ leagues.c.country }}</thead>
{% endfor %}
{% for n in leagues %}
<tr>
<td>{{ a.n.name }}</td>
</tr>
{% endfor %}
它给出了一个空白页。
如何定位值名称和国家/地区?
【问题讨论】:
标签: json django python-3.x for-loop jinja2