在Django模板中显示字典键 [英] Show the dictionary key in django template

查看:88
本文介绍了在Django模板中显示字典键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Django模板中显示字典键本身

Im wondering how i could show the dictionary key itself in a django template

示例字典:

resources = {'coin': coin, 'grain': grain, 'iron': iron, 'stone': stone, 'wood': wood,}

模板

<b>Coin: </b>{{ upgrade.coin }}

我要使用字典键(+一些html),而不是硬编码的硬币:"

Were i want to use the dictionary key (+some html) instead of the hard coded "Coin:"

有人可以帮我吗?

推荐答案

使用 dict.items 的> for 标记您要打印所有键/值对:

Use for tag with dict.items if you want to print all key/value pairs:

{% for key, value in resources.items %}
    <b>{{ key }}: </b>{{ value }}
{% endfor %}

这篇关于在Django模板中显示字典键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆