Question

I have a variable named messages which contains list of dictionaries that I pass to my template, which is supposed to itearate over the list and print the value for key "html" from each dictionary.

List of dictionaries

[
 '{"body": "asdada", "html": "sfdsfsfw", "id": "sdfsfsf"}',
 '{"body": "dgdgdg", "html": "sfdsfsfs", "id": "sfsfsdf"}', 
 '{"body": "dgeaeg", "html": "sfdaewaa", "id": "sfwaeaw"}'
]

Sample from Template

{% for message in messages %}
{{ message.html }}
{% endfor %}

When my webpage is rendered , this for loop prints nothing.I mean its blank in the webpage that I get as a result in my browser.I was expecting it to print

sfdsfsfw
sfdsfsfs
sfdaewaa

I tried below, but this gives TemplateSyntaxError

{% for message in messages %}
{{ message["html"] }}
{% endfor %}

Need help to figure out correct way to print out the value for key "html" . Please help!

Was it helpful?

Solution

The following is not a list of dictionary, but a list of strings.

[
 '{"body": "asdada", "html": "sfdsfsfw", "id": "sdfsfsf"}',
 '{"body": "dgdgdg", "html": "sfdsfsfs", "id": "sfsfsdf"}', 
 '{"body": "dgeaeg", "html": "sfdaewaa", "id": "sfwaeaw"}'
]

String object does not have html attribute / item;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top