代码未在Html中执行和显示(Flask)
#烧瓶代码代码未在Html中执行和显示(Flask),html,flask,Html,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',
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代码
{posts%%中的post为%s}
由{{posts.author}}于{{posts.date_posted}}
通过{post.content}
{%endfor%}
for循环正在执行,但是dict.中的值没有显示,我对flask非常陌生
所以我很确定我必须添加一些额外的代码。。?感谢您的帮助:D在
for
循环中使用循环变量post
,而不是posts
{%for posts in posts%}
作者{{post.author}}于{post.date_posted}}
通过{post.content}
{%endfor%}
<!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>