字典项不会从Python显示到Jinja2

时间:2016-02-04 09:59:24

标签: python html jinja2

此代码工作了一段时间然后突然停止,我无法弄清楚发生了什么。

无论我得到什么网页,除了显示超链接之外的所有内容。有4个点,以及h1和正确的标题,

我假设因为应该有四个链接。但是那里的单词/链接没有显示出来。

Python代码:

import flask
from flask import Flask
import glob
import yaml

app = Flask(__name__)

plays = {}

###load data from fn file
for fn in glob.glob('data/*.yaml'):
    with open(fn, 'r') as yf:
        play = yaml.load(yf)
        plays[play['id']] = play

for id, info in plays.items():
    print('The info for {} is {}.'.format(id, info))

@app.route('/')
def showPlayList():
    return flask.render_template('shake_plays.html',         plays=plays.values())


if __name__ == '__main__':
    app.run()

Jinja / html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Plays of Shakespeare</title>
</head>
<body>
<h1>Plays of Shakespeare</h1>
    <ul>
        {% for id, play in plays.items() %}
            <li><a href="{{ id }}"> {{ play.title }} </a></li>
        {% endfor %}
    </ul>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要将plays dict传递给模板

@app.route('/')
def showPlayList():
    return flask.render_template('shake_plays.html', plays=plays)