我希望其他人向我解释我的Python代码到底出了什么问题

2024-04-26 01:00:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我基本上是从youtube教程中复制粘贴了这段代码,对我来说,这根本不起作用,我自己也无法理解,而且对编码非常陌生也没有帮助

my.py文件中的代码:

from flask import Flask, render_template
app = Flask(__name__)
posts = [
    {
        'author': 'Corey Schafer',
        'title': 'Blog Post 1',
        'content': 'First post content',
        'date_posted': 'April 20, 2018'
    },
    {
        'author': 'Jane Doe',
        'title': 'Blog Post 2',
        'content': 'Second post content',
        'date_posted': 'April 21, 2018'
    }
]
@app.route("/")
@app.route("/home")
def home():
    return render_template('home.html', posts=posts)
@app.route("/about")
def about():
    return render_template('about.html')
if __name__ == '__main__':
    app.run(debug=True)

my.html文件中的代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    {% for post in posts %}
        <h1>{{ post.title }}</h1>
        <p>By {{ post.author }} on {{ post.date_posted }}</p>
        <p>{{ post.content }}</p>
    {% endfor %}
</body>
</html>

我收到的错误消息: “jinja2.exceptions.TemplateSyntaxError:应为标记名”


热门问题