Python 在 Flask 中使用 Jinja2 获取嵌套的字典项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24727977/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:05:13  来源:igfitidea点击:

Get nested dict items using Jinja2 in Flask

pythondictionaryflaskjinja2

提问by MattO

for this dictionary with this Flask controller

对于带有此 Flask 控制器的字典

projects = {
        'life-calc':{'url':'life-calc',
                    'title': 'Life Calculator'},
        'text-game':{'url':'text-game',
                    'title':'Text Adventure'},
        'fill-it-up':{'url':'fill-it-up',
                    'title':'Fill It Up'},
        'rock-paper-scissors':{'url':'rock-paper-scissors',
                    'title':'Rock, Paper, Scissors'},
        'bubble-popper':{'url':'bubble-popper',
                    'title':'Bubble Popper'}
            }


@app.route('/')
def index():
    return render_template("index.html",
                            projects = projects)

and the template as such

和模板

    <h1>
        List of My Projects
    </h1>

    <ol>
        <li>
            <a href = "life-calc">Life Calculator</a>
        </li>
        <li>
            <a href = "text-game">Adventure Game</a>
        </li>
        <li>
            <a href = "fill-it-up">Fill It Up</a>
        </li>
        <li>
            <a href = "rock-paper-scissors">Rock Paper Scissors</a>
        </li>
        <li>
            <a href = "bubble-popper">Bubble Popper</a>
        </li>
    </ol>
    <p>test section below</p>
    <ol>
        {% for project in projects %}
        <li><a href = "{{ project['url'] }}">{{ project['title'] }}</a> </li>
        {% endfor %}
    </ol>

{% endblock %}

How can I access the items in the dict to print a list of my projects as in the HTML above the test?

如何访问 dict 中的项目以打印我的项目列表,如测试上方的 HTML 所示?

I solved my own problem with help from Rendering a python dict in Jinja2 / WerkzeugThe template block should be

在 Jinja2 / Werkzeug 中 Rendering a python dict 的帮助下解决了我自己的问题模板块应该是

{% for key, value in projects.iteritems() %}
<li><a href={{value['url']}}>{{value['title']}}</a></li>
{% endfor %}

But I'm still curious as to how to access further nested dictionaries, and if this is the smartest way to create a simple menu.

但是我仍然很好奇如何访问更多嵌套的字典,以及这是否是创建简单菜单的最聪明的方法。

采纳答案by Nava

I think you want to know how access the nested dict in template

我想你想知道如何访问模板中的嵌套字典

If you think I got your question

如果你认为我得到了你的问题

Generally, This is the way to access the nested dictionary items in dictionary.

通常,这是访问字典中嵌套字典项的方式。

If the iterables are getting nested further just you have to increase the forloopdepth level whether it is list or dict.

如果可迭代对象进一步嵌套forloop,则无论是列表还是字典,您都必须增加深度级别。

Here I am giving just a generic example in my own way for your understanding

在这里,我仅以我自己的方式给出一个通用示例,以供您理解

Data:

数据:

parent_dict = {1: {'A':'val1','B':'val2'}, 2:{'C':'val3','D':'val4'}}

iteration in jinja2:

jinja2中的迭代:

{% for key,parent_dict_item in parent_dict.items() %}
   {% for key2, nested_value in parent_dict_item.items() %}
      <li><a href = "{{ nested_value }}">{{ nested_value }}</a> </li>
   {% endfor %}
{% endfor %}

Answer:

回答:

<li><a href="val1">val1</a> </li>
<li><a href="val2">val2</a> </li>
<li><a href="val3">val3</a> </li>
<li><a href="val4">val4</a> </li>

回答by tkteun

Instead of expanding the key and value in the loop, you can also use the key to reference the item in the dict itself:

除了在循环中扩展键和值之外,您还可以使用键来引用字典本身中的项目:

{% for project in projects %}
  <li><a href = "{{ projects[project].url }}">{{ projects[project].title }}</a> </li>
{% endfor %}