Reputation: 423
this is my template, data is a list of dictionries i wanna reach keys and values of each dictionnaries
<ul>
{% for item in data %}
{% for key in item.keys %}
{% if key == 'Server Name' %}
<li{% if forloop.last %} class='last'{% endif %}>{{ item[key] }}</li>
{% endif %}
{% if key == 'Server Price' %}
<li{% if forloop.last %} class='last'{% endif %}>{{ item[key] }}</li>
{% endif %}
{% endfor %}
</ul>
help me please
Upvotes: 0
Views: 149
Reputation: 3133
You can iterate over key-value pair the same way you would do in a for loop in python code:
<ul>
{% for item in data %}
{% for key, value in item.items %}
{% if key == 'Server Name' %}
<li{% if forloop.last %} class='last'{% endif %}>{{ value }}</li>
{% endif %}
{% if key == 'Server Price' %}
<li{% if forloop.last %} class='last'{% endif %}>{{ value }}</li>
{% endif %}
{% endfor %}
</ul>
Upvotes: 0
Reputation: 47222
Will this work for you?
The template allows you to loop over the dict as you would in any other Python code. Which is very beneficial!
<ul>
{% for key, value in data.items %}
{% if key == 'Server Name' %}
<li{% if forloop.last %} class='last'{% endif %}>{{ value }}</li>
{% endif %}
{% if key == 'Server Price' %}
<li{% if forloop.last %} class='last'{% endif %}>{{ value }}</li>
{% endif %}
{% endfor %}
</ul>
Or am i missing something?
Upvotes: 0
Reputation: 62948
Just use items
:
<ul>
{% for item in data %}
{% for key, value in item.items %}
{% if key == 'Server Name' %}
<li{% if forloop.last %} class='last'{% endif %}>{{ value }}</li>
{% endif %}
{% if key == 'Server Price' %}
<li{% if forloop.last %} class='last'{% endif %}>{{ value }}</li>
{% endif %}
{% endfor %}
</ul>
PS: you sure you didn't mean forloop.parentloop.last
?
PPS: is there no way you can redesign your dicts so the keys don't have spaces in them?
Upvotes: 1