我想通过 Flask 将 json 格式的数据插入到 web 应用程序中,以将值放入 html 中:
spec_player.html:
{% for p in posts: %}
<p>{{p.first_name}}</p>
{% endfor %}
这有效(main.py):
posts = [
{
"id":237,"first_name":"LeBron","height_feet":6,"height_inches":8,"last_name":"James","position":"F",
"team":{"id":14,"abbreviation":"LAL","city":"Los Angeles","conference":"West","division":"Pacific","full_name":"Los Angeles Lakers","name":"Lakers"},"weight_pounds":250
}
]
@app.route("/spec")
def spec_player():
return render_template("spec_player.html", posts=posts)
这不起作用(main.py):
posts = [
{
"data":[{"id":237,"first_name":"LeBron","height_feet":6,"height_inches":8,"last_name":"James","position":"F",
"team":{"id":14,"abbreviation":"LAL","city":"Los Angeles","conference":"West","division":"Pacific","full_name":"Los Angeles Lakers","name":"Lakers"},"weight_pounds":250}],
"meta":{"total_pages":1,"current_page":1,"next_page":null,"per_page":25,"total_count":1}
}
]
@app.route("/spec")
def spec_player():
return render_template("spec_player.html", posts=posts)
我想知道是否有办法将 2.json-format 转换为 1.format?(我只从 api 获取 2.json 格式) 或者 在 html 中编写其他查询(类似于 p.data.first_name)?
胡说叔叔
四季花海
相关分类