python - list of dictionary in jinja template -
how iterate through list of dictionaries in jinja template?
list1=[{"username": "abhi","pass": 2087}] return render_template("file_output.html",lis=list1) in template
<table border=2> <tr> <td> key </td> <td> value </td> </tr> {% lis1 in lis %} {% key in lis1 %} <tr> <td> <h3>{{key}}</h3> </td> <td> <h3>{{lis1[key]}}</h3> </td> </tr> {% endfor %} {% endfor %} </table> the above code splitting each element multiple
key value [
{
"
u
s
e ...
i tested above nested loop in simple python script , works fine not in jinja template.
data:
parent_dict = [{'a':'val1','b':'val2'},{'c':'val3','d':'val4'}] in jinja2 iteration:
{% dict_item in parent_dict %} {% key, value in dict_item.items() %} <h1>key: {{key}}</h1> <h2>value: {{value}}</h2> {% endfor %} {% endfor %} note:
make sure have list of dict items.if unicodeerror may value inside dict contains unicode format. issue can solved in views.py if dict unicode object, have encode utf-8
Comments
Post a Comment