最简单的按钮不起作用

Most simple button doesn't work

我在 HTML 中有一个非常简单的提交按钮,但它不起作用。

main_page.html

<form class="main_page" method="POST" action=".">
    <div class="form-row">
         <input type="submit" name="invoer" value="Invoeren"/>
    </div>
</form>

flask_app.py

from flask import Flask, render_template, request

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/", methods=["GET", "POST"])

def main():
    if request.method == "GET":
        return render_template("main_page.html")

    if request.form["invoer"] == "POST":
        return render_template("main_page.html")

当我点击按钮时,它会显示:

-------------------------------------------- ---------------------------------------------- ----------------------------------


我以前做过这样的按钮,但后来它起作用了,也许我做了一些不同的事情。我该怎么办?

错误显示 ValueError: View function did not return a response,这意味着当您单击 HTML 中的提交按钮时,您的 POST 没有 return 对模板做出任何响应。将您的代码 flask_app.py 更改为:

from flask import Flask, render_template, request

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/", methods=["GET", "POST"])

def main():
    if request.method == "GET":
        return render_template("main_page.html")

    if request.method == "POST": # change code here
        return render_template("main_page.html")

这将使它起作用,但是,它将return只是同一个模板。