将变量从 flask 传递到 html 时我做错了什么
What am I doing wrong when passing the variable from flask to html
我正在尝试创建一个用于 Flask 实践的博客。我正在通过 mysql.connector 连接到 mysql 数据库。 table 名称是 'posts' 。 Table 有 id、title、content 和 author 列。除了 jinja 语法 {{ post.title }} 没有显示 post 标题(标题列的数据)
任何人都可以帮助我在 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}} 数据没有显示数据库 table 中的 post 标题。
我不明白为什么。我做错了什么?
会不会是函数名和返回变量名一样?
我正在尝试创建一个用于 Flask 实践的博客。我正在通过 mysql.connector 连接到 mysql 数据库。 table 名称是 'posts' 。 Table 有 id、title、content 和 author 列。除了 jinja 语法 {{ post.title }} 没有显示 post 标题(标题列的数据) 任何人都可以帮助我在 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}} 数据没有显示数据库 table 中的 post 标题。 我不明白为什么。我做错了什么?
会不会是函数名和返回变量名一样?