问题描述
我正在尝试创建一个有关烧瓶练习的博客。我正在通过MysqL.connector连接到MysqL数据库。表名称为'posts'。表格具有ID,标题,内容和作者列。除了jinja语法{{post.title}}之外的所有其他东西都没有显示帖子标题(title列的数据) 任何人都可以帮我在Jinja语法中做错什么,还是将变量从flask传递到html。
这是我的烧瓶代码:
@app.route('/posts',methods=['GET','POST'])
def posts():
db = MysqL.connector.connect(host='127.0.0.1',user='shehan',password='shehan',database='blog')
mycursor = db.cursor()
mycursor.execute("SELECT * FROM posts")
all_posts = mycursor.fetchall()
return render_template('posts.html',ts=all_posts)
if __name__ == "__main__":
app.run(debug=True)```
------------
{% for t in ts %}
<h2>Title: {{ t.title }}</h2>
<h3>Post: {{ t.content }}</h3>
<p>By: {{ t.author }}</p>
{% endfor %}
浏览器未显示任何错误。除{{post.title}}数据未显示数据库表中的帖子标题外,它都工作正常。 我不明白为什么。我在做什么错了?
解决方法
函数名称是否与返回变量的名称相同?