[英]Code not executing and displaying in Html (Flask)
#Flask Code #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 Code #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>
the for loop is executing but the values in the dict. for 循环正在执行,但字典中的值。 are not displaying, I am very new in flask so I'm pretty sure I have to add in some extra code..?没有显示,我在 flask 中很新,所以我很确定我必须添加一些额外的代码..? Any help is appreciated:D任何帮助表示赞赏:D
Use your loop variable post
inside your for
loop instead of posts
.在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.