Home > Enterprise >  Django Jinja template display value from dictionary key
Django Jinja template display value from dictionary key

Time:05-17

I have problem to display data in jinja template. I have a dictionary which looks like:

data = {
  'Game1': {'win': 0, 'lost': 2, 'all': 2},
  'Game2': {'win': 1, 'lost': 0, 'all': 1},
  'Game3': {'win': 2, 'lost': 0, 'all': 2}
}

This dictionary is passed to template as game_details. I want to display this data like:

Game:  Game1
Win:  0
Lost:  2
All:  2
Game:  Game2
Win:  1
Lost:  0
All:  1
Game:  Game3
Win:  2
Lost:  0
All:  2

Using python there is no problem, because I can call data by key, but in my template I'm trying to call them like:

    {% for key, value in game_details.items %}
        Game: {{ key }}<br/>
        Win: {{ value['win'] }}
    {% endfor %}

which leads to TemplateSyntaxError. Could not parse the remainder: '['win']' from 'value['win']'. How can I call a specific value from dictionary using key in jinja template?

CodePudding user response:

Try:

{% for key, value in game_details.items %}
    Game: {{ key }}<br/>
    Win: {{ value.win }}
{% endfor %}
  • Related