我的问题是,我似乎无法将字典翻译成表格,这是错误:
Could not parse ["appid"] from 'game[''appid"]
HTML code:
<table>
<tr>
<th>Game ID</th>
<th>Game Name</th>
<th>Hours Played</th>
</tr>
{% for game in games %}
{# each game object is a dictionary with "appid", "name " and "playtime_forever" keys #}
<tr>
<td>{{ game["appid"] }}</td>
<td>{{game["name"]}}</td>
<td>{{ game["playtime_forever"] }}</td>
</tr>
</table>
views.py代码:
~~~~ There's stuff here but it shouldn't be important. ~~~~
return render(request,'hackathon/SteamAPI.html', game)
当我运行服务器时,它会显示:
游戏:
[{u'appid': 4000,
u'has_community_visible_stats': True,
u'img_icon_url': u'd9101cbeddcc4ff06c7fa1936c3f381b0bbf2e92',
u'img_logo_url': u'dca12980667e32ab072d79f5dbe91884056a03a2',
u'name': u"Garry's Mod",
u'playtime_forever': 0},
答案 0 :(得分:3)
Django模板不支持[]
索引。而不是
game["appid"]
你应该使用
game.appid
同样适用于game.name
和game.playtime_forever
作为无关的说明,您还应关闭for
循环:
</tr>
{% endfor %}
</table>
答案 1 :(得分:0)
应该在Django模板中以这种方式访问词典。
choices = {'key1':'val1','key2':'val2'}
<ul>
{% for key, value in choices.items %}
<li>{{key}} - {{value}}</li>
{% endfor %}
</ul>