[英]Code not executing and displaying in Html (Flask)
#Flask 代碼
from flask import Flask, render_template
app = Flask(__name__)
posts = [
{
'author': 'User',
'title': 'Test',
'content': 'First post',
'date_posted': '2021, 4 ,13',
},
{
'author': 'User2',
'title': 'Flask is cool',
'content': 'Flask testing',
'date_posted': '2021, 4 ,14'
}
]
@app.route('/')
@app.route('/home')
def hello():
return render_template('home.html', posts=posts)
#ignore this
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)
#HTML 代碼
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{% for post in posts %}
<p>By {{ posts.author }} on {{ posts.date_posted }}</p>
<p>By {{ post.content }}</p>
{% endfor %}
</body>
</html>
for 循環正在執行,但字典中的值。 沒有顯示,我在 flask 中很新,所以我很確定我必須添加一些額外的代碼..? 任何幫助表示贊賞:D
在for
循環中使用循環變量post
而不是posts
。
{% for post in posts %}
<p>By {{ post.author }} on {{ post.date_posted }}</p>
<p>By {{ post.content }}</p>
{% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.