我在使用 Genshipy:for
属性时遇到问题。我做错了什么?代码写在下面;运行,使用 Python 2 创建一个 virtualenv pip install genshi flask
,复制文件,复制一个隔离目录中列出的文件,然后运行python hello.py
编码
内容hello.py
:
import os.path
import traceback
import flask
import genshi.template
app = flask.Flask(__name__)
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
loader = genshi.template.TemplateLoader(template_dir, auto_reload=True)
MESSAGES = [
"Hello",
"World",
"Sup?",
]
@app.route("/", defaults={"name": ""})
@app.route("/<path:name>")
def show(name):
template_name = name + ".html"
try:
template = loader.load(template_name)
stream = template.generate(
messages=MESSAGES,
)
rendered = stream.render('html', doctype='html')
except Exception as e:
tb = traceback.format_exc()
return "Cannot load /{}: {} <pre>\n{}</pre>".format(name, e, tb)
return rendered
if __name__ == '__main__':
app.run()
内容templates/debug.html
:
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://geshi.edgewall.org/"
>
<body>
<p>Messages is a ${str(type(messages))} of length ${len(messages)}</p>
<p>Messages:</p>
<pre>
${'\n'.join(m + "!" for m in messages)}
</pre>
</body>
</html>
内容templates/hello.html
:
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://geshi.edgewall.org/"
>
<body>
<h1>Messages</h1>
<ul>
<li py:for="msg in messages">
$msg
</li>
</ul>
</body>
</html>
问题
当我访问时http://localhost:5000/debug
,一切似乎都按预期工作,但是当我运行时,http://localhost:5000/hello
我得到“无法渲染/hello:'msg'未定义”