EDIT Upon finding the solution, I changed the title to better reflect the issue. Unstuckify's reply remains valid and relevant to the text of the question
I have a list of dictionaries that I want to loop through with Jinja2:
dict = [{'symbol': 'BTCUSDT', 'price': 59325.1234, 'Qty': 2501}, {'symbol': 'ETHUSDT', 'price': 55.12, 'Qty': 14}]
I've used the loop below (from here). I expected the outer 'for' statement to pick up the first item in the list (which would be a dict) and the inner 'for' loop to iterate through the k,v in the dict.
{% for dict_item in dict %}
{% for key, value in dict_item.items() %}
<h1>Key: {{key}}</h1>
<h2>Value: {{value}}</h2>
{% endfor %}
{% endfor %}
However, Jinja gives me the following error - which suggests Jinja doesn't recognise the elements in the list as dicts:
jinja2.exceptions.UndefinedError: 'list object' has no attribute 'items'
Even heavily simplifed I keep getting the same error:
{% for symbol in dict %}
<h2>{{ symbol }}</h2>
{% endfor %}
The Jinja docs aren't detailed enough on this. I've also tried this and looked at this approach without success. Passing just a dict (not a list of dicts) works well.
Can anybody explain why I'm getting this error and suggest better code? Getting the same error message with different approaches leads me to think there's some fundamental error in my thinking, but I can't figure it out.