【问题标题】:Iterating through a python dictionary in flask jinja迭代烧瓶神社中的python字典
【发布时间】:2021-12-08 03:37:41
【问题描述】:

我有一本包含所有国家代码和相应国家名称的字典,它的示例如下所示:

{'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)

【问题讨论】:

    标签: python dictionary flask iteration jinja2


    【解决方案1】:

    views.py 中,您将countries=countriess 设置为模板渲染。 countriesstest 函数 (countriess = {}) 的 for 循环中重新初始化,因此传递给模板的 countriess 实际上是 {country_code: country_name} 对,用于来自 @ 的最后一个国家/地区987654328@名单。

    回到实际的错误:当您在模板 ({% for dict_item in countries %}) 中迭代 countries 字典时,实际上是在迭代 countries 的键,正如我之前所说,它是来自 countriess views.py,所以基本上你只需从countries 检索最后一个国家/地区的country_code。所以 dict_item 实际上是一个字符串(国家代码),因此您会收到{% for key,value in dict_item.items %} 的错误,认为它实际上是字典而不是字符串。

    TL;DR 我认为您的意思是在views.py 中执行countries=countries 而不是countries=countriess。然后剩下的代码就有意义了。 (我假设 countriess 的 for 循环只是为了调试?)

    【讨论】:

    • 我尝试了您的更改,但似乎收到了错误:jinja2.exceptions.UndefinedError: 'website.models.Country object' has no attribute 'items'
    • 哦,好吧,这都是关于类型的。我认为countries 可能是一本字典,但它是一个单独的 Country 类。在这种情况下,您确实必须根据需要从 country 的值中创建一个字典(如已接受的答案中所建议的那样)。
    【解决方案2】:

    尝试将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,无需更改模板代码。

    【讨论】:

    • 你好,这个很好用,国家是列表还是字典?
    • countriess 是一个字典列表
    • 好吧,这是有道理的。我有一个下拉菜单,我想将所有国家都添加到其中。 ``` {% for dict_item in countries %} {% for key, value in dict_item.items() %}
      {% endfor %} {% endfor %} ``` 可以你请解释为什么这没有添加任何东西。在这里可能更容易阅读:pastebin.com/wxQmHuS7
    • 对不起,我不能说
    • 抱歉,我以为我回复了,我犯了一个愚蠢的错误,但您的解决方案非常有效。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2020-02-21
    • 2020-04-27
    • 1970-01-01
    • 2016-04-20
    相关资源
    最近更新 更多