I tried:
list1 = [{"username": "abhi", "pass": 2087}]
return render_template("file_output.html", list1=list1)
在模板中:
<table border=2>
<tr>
<td>
Key
</td>
<td>
Value
</td>
</tr>
{% for dictionary in list1 %}
{% for key in dictionary %}
<tr>
<td>
<h3>{{ key }}</h3>
</td>
<td>
<h3>{{ dictionary[key] }}</h3>
</td>
</tr>
{% endfor %}
{% endfor %}
</table>
上面的代码将每个元素拆分为多个字符:
[
{
"
u
s
e
r
...
我在一个简单的 Python 脚本中测试了上面的嵌套循环,它工作正常,但在 Jinja 模板中不行。
Data:
parent_list = [{'A': 'val1', 'B': 'val2'}, {'C': 'val3', 'D': 'val4'}]
在 Jinja2 迭代中:
{% for dict_item in parent_list %}
{% for key, value in dict_item.items() %}
<h1>Key: {{key}}</h1>
<h2>Value: {{value}}</h2>
{% endfor %}
{% endfor %}
Note:
确保您有字典项目列表。如果你得到UnicodeError
可能是 dict 内的值包含 unicode 格式。这个问题可以在您的views.py
。
如果字典是unicode
对象,你必须编码成utf-8
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)