【发布时间】:2020-10-16 22:57:14
【问题描述】:
我想在我的页面上列出嵌套 JSON 的内容。要实现这一点,需要对 awesome.html 进行哪些更改?
不指定特定子类别就不能一次列出所有内容吗?
更具体地说,awesome.html 中的这部分代码不起作用:
{% for each_category in content -%}
{{ each_category }} {{ content[each_category].each_category.items() }}
{% endfor %}
我尝试在 for 循环中使用第二个 for,但根本不起作用:
{% for each_category in content -%}
{{ each_category }}
{% for each_subcategory in each_category -%}
{{ each_subcategory }} {{ content[each_subcategory].port }}
{% endfor %}
{% endfor %}
此代码有效,但它仅列出端口,而我想要所有内容,而不仅仅是端口:
{% for each_category in content -%}
{{ each_category }} {{ content[each_category].port }}
{% endfor %}
额外信息
configs/env.json 的示例内容:
{
"env" : {
"db" : {
"postgres_db_useradmin" : "devadmin",
"postgres_db_useradmin_pw" : "sdfkjsnbdfjkgb",
"port" : "2134",
"app" : "sdfszdg"
},
"ssh" : {
"ssh_user" : "ubuntu",
"ssh_server" : "website.com",
"port" : "22"
},
"app" : {
"ht_protocol" : "http",
"port" : "8080"
}
}
}
在 app.py 中加载 JSON,片段
with open('configs/env.json', 'r') as json_file:
env_dict = json.load(json_file)
content = env_dict['env'] # removing the top env category, because it is the only one
awesome.html 使用 jinja 语法需要更改的内容:
{% extends 'index.html' %}
{% block content %}
<h1>Awesome page</h1>
{% for each_category in content -%}
{{ each_category }} {{ content[each_category].each_category.items() }}
{% endfor %}
{% endblock %}
我发现了类似的问题,但无法根据我的情况调整解决方案:
感谢您的帮助!
【问题讨论】:
标签: python json dictionary flask jinja2