Python 如何遍历 Jinja 模板中的字典列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25373154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate through a list of dictionaries in Jinja template?
提问by user3089927
I tried:
我试过:
list1 = [{"username": "abhi", "pass": 2087}]
return render_template("file_output.html", list1=list1)
In the template:
在模板中:
<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>
The above code is splitting each element into multiple characters:
上面的代码是将每个元素拆分成多个字符:
[
{
"
u
s
e
r
...
I tested the above nested loop in a simple Python script and it works fine but not in Jinja template.
我在一个简单的 Python 脚本中测试了上面的嵌套循环,它运行良好,但在 Jinja 模板中不起作用。
采纳答案by Nava
Data:
数据:
parent_list = [{'A': 'val1', 'B': 'val2'}, {'C': 'val3', 'D': 'val4'}]
in Jinja2 iteration:
在 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:
笔记:
Make sure you have the list of dict items. If you get UnicodeErrormay be the value inside the dict contains unicode format. That issue can be solved in your views.py.
If the dict is unicodeobject, you have to encode into utf-8.
确保您有 dict 项目列表。如果你得到的UnicodeError可能是 dict 里面的值包含 unicode 格式。该问题可以在您的views.py. 如果 dict 是unicode对象,则必须编码为utf-8.
回答by corvid
{% for i in yourlist %}
{% for k,v in i.items() %}
{# do what you want here #}
{% endfor %}
{% endfor %}
回答by Chris.Q
As a sidenote to @Navaneethan 's answer, Jinja2is able to do "regular" item selections for the list and the dictionary, given we know the key of the dictionary, or the locations of items in the list.
作为@Navaneethan 答案的旁注Jinja2,假设我们知道字典的键或列表中项目的位置,可以为列表和字典进行“常规”项目选择。
Data:
数据:
parent_dict = [{'A':'val1','B':'val2', 'content': [["1.1", "2.2"]]},{'A':'val3','B':'val4', 'content': [["3.3", "4.4"]]}]
in Jinja2 iteration:
在 Jinja2 迭代中:
{% for dict_item in parent_dict %}
This example has {{dict_item['A']}} and {{dict_item['B']}}:
with the content --
{% for item in dict_item['content'] %}{{item[0]}} and {{item[1]}}{% endfor %}.
{% endfor %}
The rendered output:
渲染输出:
This example has val1 and val2:
with the content --
1.1 and 2.2.
This example has val3 and val4:
with the content --
3.3 and 4.4.
回答by otterb
Just a side note for similar problem (If we don't want to loop through):
只是类似问题的附注(如果我们不想循环):
How to lookup a dictionary using a variable key within Jinja template?
如何使用 Jinja 模板中的变量键查找字典?
Here is an example:
下面是一个例子:
{% set key = target_db.Schema.upper()+"__"+target_db.TableName.upper() %}
{{ dict_containing_df.get(key).to_html() | safe }}
It might be obvious. But we don't need curly braces within curly braces. Straight python syntax works. (I am posting because I was confusing to me...)
这可能是显而易见的。但是我们不需要花括号内的花括号。直接的 python 语法有效。(我发帖是因为我让我感到困惑......)
Alternatively, you can simply do
或者,你可以简单地做
{{dict[target_db.Schema.upper()+"__"+target_db.TableName.upper()]).to_html() | safe }}
But it will spit an error when no key is found. So better to use getin Jinja.
但是找不到key的时候会报错。所以最好get在 Jinja 中使用。

