如何在烧瓶中使用for循环显示JSON数据

时间:2019-06-28 20:39:17

标签: python json flask

我试图使用{% for bring in company %} ... {% endfor %}循环使用flask在flask中显示JSON数据,但是没有成功。但是,我已经使用直接应用程序将JSON应用于about.html页面,其数据/名称如下:{ {1}},并且有效。我不知道是什么问题

我什至试图找到它,但没有找到任何解决方案。

Python /烧瓶

{{races[0][name]}}

about.html

import os
import json
from flask import Flask, render_template
app = Flask(__name__)


@app.route("/")
def index():
    return render_template("index.html")

@app.route('/about')
def about():
    data = []
    with open("data/races.json", "r") as json_data:
        data = json.load(json_data)
    return render_template("about.html", page_title="About", races=data)

@app.route("/contact")
def contact():
    return render_template("contact.html", page_title="Contact")

@app.route("/careers")
def careers():
    return render_template("careers.html", page_title="Careers")


if __name__ == "__main__":
    app.run(host=os.environ.get("IP"),
            port=int(os.environ.get("PORT")),
            debug=True)

JSON

{% extends 'base.html' %} {% block content %}

<h2>{{ page_title}}</h2>
<p>
    StarCraft is a military science fiction media franchise, created by Chris Metzen and James Phinney and owned by Blizzard Entertainment. The series, set in the beginning of the 26th century, centers on a galactic struggle for dominance among four species—the
    adaptable and mobile Terrans, the ever-evolving insectoid Zerg, the powerfully enigmatic Protoss, and the "god-like" Xel'Naga creator race—in a distant part of the Milky Way galaxy known as the Koprulu Sector. The series debuted with the video game
    StarCraft in 1998. It has grown to include a number of other games as well as eight novelizations, two Amazing Stories articles, a board game, and other licensed merchandise such as collectable statues and toys.
</p>

{% for member in company %}
<div class="row featurette">
    <div class="col-md-7">
        <h3>{{member.name}}</h3>
        <p>
            {{member.description}}
        </p>
    </div>
    <div class="col-md-5">
        <img class="featurette-image img-responsive" src="{{member.image_source}}"alt="Picture of {{ member.name }}"></img>
    </div>
</div>
<hr class="featurette-divider">

 {% endfor %}

{% endblock %}

正如我之前提到的,一旦在JSON对象中只有一个项目(不是三个项目),它就可以正常工作,但是我几乎可以确定这不是问题。现在,当我运行循环显示时,循环中的所有HTML都不会出现在屏幕上。

2 个答案:

答案 0 :(得分:1)

您将数据以races而不是company的形式发送到模板。

答案 1 :(得分:1)

尝试about.html:

{% extends 'base.html' %} {% block content %}

<h2>{{ page_title}}</h2>
<p>
    StarCraft is a military science fiction media franchise, created by Chris Metzen and James Phinney and owned by Blizzard Entertainment. The series, set in the beginning of the 26th century, centers on a galactic struggle for dominance among four species—the
    adaptable and mobile Terrans, the ever-evolving insectoid Zerg, the powerfully enigmatic Protoss, and the "god-like" Xel'Naga creator race—in a distant part of the Milky Way galaxy known as the Koprulu Sector. The series debuted with the video game
    StarCraft in 1998. It has grown to include a number of other games as well as eight novelizations, two Amazing Stories articles, a board game, and other licensed merchandise such as collectable statues and toys.
</p>

{% for member in races %}
<div class="row featurette">
    <div class="col-md-7">
        <h3>{{member.name}}</h3>
        <p>
            {{member.description}}
        </p>
    </div>
    <div class="col-md-5">
        <img class="featurette-image img-responsive" src="{{member.image_source}}"alt="Picture of {{ member.name }}"></img>
    </div>
</div>
<hr class="featurette-divider">

 {% endfor %}

{% endblock %}