Flask error: Not being able to add blocks in a simple Flask application

Flask error: Not being able to add blocks in a simple Flask application

我正在尝试创建一个使用模板中的块的简单 Flask 应用程序。当我尝试从我的 CS50 ide 运行 这个时,我得到一个 500 Internal Server Error。我在 Web Tracks 中关注 CS50 的 Flask 教程,看来我写的内容与 video 完全相同。 这是我的 application.py 文件:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")


@app.route("/hello")
def hello():
    name = request.args.get("name")
    if not name:
        return render_template("failure.html")
    return render_template("hello.html", name=name)

这是我的 html 页:

layout.html

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>Hello!</title>
    </head>
    <body>
        {% block body %}
        {% endblock %}
    </body>
</html>

这是我的 index.html(我不会包括失败和问候页面,因为它们给出完全相同的错误并且它们几乎是同一件事):

{% extends "layout.html" %}

{ block body %}
        <form action="/hello">
            <input name="name" type="text">
            <input type="submit">
        </form>
{% endblock %}

错误信息: jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endblock'.

我认为您在 index.html 的第 3 行中遗漏了一个 %。我查阅了 here 以供参考。

{% extends "layout.html" %}

{% block body %}
        <form action="/hello">
            <input name="name" type="text">
            <input type="submit">
        </form>
{% endblock %}