Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show the dictionary key in django template

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

Example dictionary:

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

Template

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

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

Can anyone please help me out?

like image 801
Hans de Jong Avatar asked Jan 06 '14 18:01

Hans de Jong


1 Answers

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 %}
like image 156
falsetru Avatar answered Sep 21 '22 02:09

falsetru