遍历 flask jinja 中的 python 字典
Iterating through a python dictionary in flask jinja
我有一本字典,里面有所有的国家代码和相应的国家名称,它的一个例子是这样的:
{'AF': 'Afghanistan'}
{'AL': 'Albania'}
{'DZ': 'Algeria'}
{'AD': 'Andorra'}
{'AO': 'Angola'}
当我按照这个堆栈溢出问题:How to iterate through a list of dictionaries in Jinja template? 尝试遍历国家时,我遇到了一个问题,因为它没有添加任何元素。
这是我的代码:
{% extends "base.html" %} {% block title %}Test{% endblock %}
{% block content %}
<div class="container pt-5">
<h1 align="center">TEST PAGE</h1>
</div>
{% for dict_item in countries %}
{% for key,value in dict_item.items %}
<h1>Key: {{ key }}</h1>
<h2>Value: {{ value }}</h2>
{% endfor %}
{% endfor %}
{% endblock %}
它没有添加任何标题,当我尝试 dict_items.items()
(项目后面有括号)时,我得到了一个错误:jinja2.exceptions.UndefinedError: 'str object' has no attribute 'items'
我不太确定出了什么问题。任何帮助将不胜感激。
(万一有用,这是我的views.py:)
@views.route("/test", methods=["GET"])
@login_required
def test():
countries = Country.query.all()
for country in countries:
countriess = {}
countriess[country.country_code] = country.country_name
print(countriess)
return render_template("TEST.html", user=current_user, countries=countriess)
尝试将 views.py
更改为:
@views.route("/test", methods=["GET"])
@login_required
def test():
countries = Country.query.all()
countriess = []
for country in countries:
countriess.append({country.country_code: country.country_name})
return render_template("TEST.html", user=current_user, countries=countriess)
此代码将创建字典列表countries
,无需更改模板代码。
在 views.py
中,您设置 countries=countriess
用于模板渲染。 countriess
在 test
函数(countriess = {})的 for
循环中重新初始化,因此传递给模板的 countriess
实际上是 {country_code: country_name}
对 countries
列表中的最后一个国家/地区。
回到实际的错误:当您在模板 ({% for dict_item in countries %}
) 中迭代 countries
字典时,您实际上迭代了 countries
的键,正如我之前说过,是views.py
中的countriess
,所以基本上你只是从countries
中获取最后一个国家的country_code
。所以 dict_item 实际上是一个字符串(国家代码),因此你得到 {% for key,value in dict_item.items %}
的错误,认为它实际上是一个字典而不是字符串。
TL;DR 我认为您打算在 views.py
中执行 countries=countries
而不是 countries=countriess
。那么剩下的代码就有意义了。 (我假设带有 countriess
的 for 循环只是为了调试?)
我有一本字典,里面有所有的国家代码和相应的国家名称,它的一个例子是这样的:
{'AF': 'Afghanistan'}
{'AL': 'Albania'}
{'DZ': 'Algeria'}
{'AD': 'Andorra'}
{'AO': 'Angola'}
当我按照这个堆栈溢出问题:How to iterate through a list of dictionaries in Jinja template? 尝试遍历国家时,我遇到了一个问题,因为它没有添加任何元素。 这是我的代码:
{% extends "base.html" %} {% block title %}Test{% endblock %}
{% block content %}
<div class="container pt-5">
<h1 align="center">TEST PAGE</h1>
</div>
{% for dict_item in countries %}
{% for key,value in dict_item.items %}
<h1>Key: {{ key }}</h1>
<h2>Value: {{ value }}</h2>
{% endfor %}
{% endfor %}
{% endblock %}
它没有添加任何标题,当我尝试 dict_items.items()
(项目后面有括号)时,我得到了一个错误:jinja2.exceptions.UndefinedError: 'str object' has no attribute 'items'
我不太确定出了什么问题。任何帮助将不胜感激。
(万一有用,这是我的views.py:)
@views.route("/test", methods=["GET"])
@login_required
def test():
countries = Country.query.all()
for country in countries:
countriess = {}
countriess[country.country_code] = country.country_name
print(countriess)
return render_template("TEST.html", user=current_user, countries=countriess)
尝试将 views.py
更改为:
@views.route("/test", methods=["GET"])
@login_required
def test():
countries = Country.query.all()
countriess = []
for country in countries:
countriess.append({country.country_code: country.country_name})
return render_template("TEST.html", user=current_user, countries=countriess)
此代码将创建字典列表countries
,无需更改模板代码。
在 views.py
中,您设置 countries=countriess
用于模板渲染。 countriess
在 test
函数(countriess = {})的 for
循环中重新初始化,因此传递给模板的 countriess
实际上是 {country_code: country_name}
对 countries
列表中的最后一个国家/地区。
回到实际的错误:当您在模板 ({% for dict_item in countries %}
) 中迭代 countries
字典时,您实际上迭代了 countries
的键,正如我之前说过,是views.py
中的countriess
,所以基本上你只是从countries
中获取最后一个国家的country_code
。所以 dict_item 实际上是一个字符串(国家代码),因此你得到 {% for key,value in dict_item.items %}
的错误,认为它实际上是一个字典而不是字符串。
TL;DR 我认为您打算在 views.py
中执行 countries=countries
而不是 countries=countriess
。那么剩下的代码就有意义了。 (我假设带有 countriess
的 for 循环只是为了调试?)