gpt4 book ai didi

python - Django html 的模板和 View 问题

转载 作者:行者123 更新时间:2023-11-30 23:13:26 26 4
gpt4 key购买 nike

我的问题是我似乎无法将字典翻译成表格,这是错误:

Could not parse ["appid"] from 'game[''appid"] 

HTML 代码:

<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},

最佳答案

Django 模板不支持 [] 索引。而不是

game["appid"] 

你应该使用

game.appid

这同样适用于 game.namegame.playtime_forever

作为一个不相关的注释,您还应该关闭 for 循环:

</tr>
{% endfor %}
</table>

关于python - Django html 的模板和 View 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29333765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com