How to iterate through a list of dictionaries in Jinja template?

后端 未结 5 1361
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 20:00

I tried:

list1 = [{\"username\": \"abhi\", \"pass\": 2087}]
return render_template(\"file_output.html\", list1=list1)

In the template:

5条回答
  •  独厮守ぢ
    2020-12-07 20:39

    Data:

    parent_list = [{'A': 'val1', 'B': 'val2'}, {'C': 'val3', 'D': 'val4'}]
    

    in Jinja2 iteration:

    {% for dict_item in parent_list %}
       {% for key, value in dict_item.items() %}
          

    Key: {{key}}

    Value: {{value}}

    {% endfor %} {% endfor %}

    Note:

    Make sure you have the list of dict items. If you get UnicodeError may be the value inside the dict contains unicode format. That issue can be solved in your views.py. If the dict is unicode object, you have to encode into utf-8.

提交回复
热议问题