JSON中的数据由2个变量组成:昵称和时间(例如{“ nick”:“ name”,“ score”:00:01:24“})。在烧瓶中,使用以下代码对其进行排序:< / p>
def sortBy(a):
return a['score']
def sort():
f=open('/home/renarsz/mysite/scores.txt', 'r')
resList=json.load(f)
f.close();
res = sorted(resList, key=sortBy)
return res
此后,在对GET进行提示时,此代码可确保列表沿
发送@app.route("/", methods=["GET"])
def index():
scores= sort()
if request.method == "GET":
return render_template("mysite/index.html", scores=scores)
return redirect(url_for('index'))
在那之后,我想在HTML中创建一个页首横幅,在表格中显示名称和时间。我以为可以用这段代码实现目标
<table>
<tr>
<th>Nickname</th>
<th>Time</th>
</tr>
{% for score in scores %}
<tr>
<td>{{score.niks}}</td>
<td>{{score.score}}</td>
</tr>
{% endfor %}
</table>
但是ir并未填充表格,而是在表格上方显示了{% for score in scores %}
和{% endfor %}
,并在第一行显示了{{score.niks}}
{{score.score}}