Flask的新手,试图尝试使用教程,但是render_template()
方法中的上下文变量遇到了一些问题。
这里是app.py
:
from flask import Flask, render_template, url_for
app = Flask(__name__)
posts = [
{
'author': 'Some Author',
'title': 'Blog Post 1',
'content': 'First blog post',
'date_posted': 'April 21, 2018'
},
{
'author': 'Another Author',
'title': 'Blog Post 2',
'content': 'Second blog post',
'date_posted': 'May 21, 2013'
}
]
@app.route("/")
@app.route("/home")
def home():
return render_template('home.html', posts=posts)
@app.route("/about")
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)
这是我的home.html
模板:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
{ % for post in posts % }
<h4>{{ post.title }}</h4>
<p>By {{ post.author }} on {{ post.date_posted }}</p>
<p>{{ post.content }}</p>
{ % endfor % }
</body>
</html>
但是以上操作无效。 在页面顶部的浏览器中,
jinja2.exceptions.UndefinedError
jinja2.exceptions.UndefinedError: 'post' is undefined
这也是来自终端的回溯:
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/bigdaddy/Sites/Flask_Blog/app.py", line 25, in home
return render_template('home.html', some_list=posts)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/templating.py", line 135, in render_template
context, ctx.app)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/templating.py", line 117, in _render
rv = template.render(context)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/bigdaddy/Sites/Flask_Blog/templates/home.html", line 8, in top-level template code
<h4>{{ post.title }}</h4>
File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'post' is undefined
任何帮助将不胜感激。 如果有帮助,请在Xubuntu Linux 16.04上的virtualenv中使用python3.6。 谢谢
答案 0 :(得分:1)
Jinja没有检测到for循环,因为您在{
和%
之间放置了一个空格,Jinja正在寻找特定的{%
对。现在,Jinja完全忽略了{ % for post in posts % }
行,将其视为HTML的一部分,无需执行任何操作,而是移至下一个找不到post
的行。
以下内容修复了循环的开始和结束:
<body>
{% for post in posts %}
<h4>{{ post.title }}</h4>
<p>By {{ post.author }} on {{ post.date_posted }}</p>
<p>{{ post.content }}</p>
{% endfor %}
</body>