1 个 html 文件中有 2 个表单会引发错误:400 错误请求(flask)

问题描述 投票:0回答:1

我最近开始学习 Flask,遇到了一个问题,当 1 个 html 文件有 2 个可以提交的表单时发生。当只有一种形式时,不会发生此问题。

这是我的包含flask的python文件:

from flask import Flask, request, render_template

app = Flask(__name__)
app.secret_key = 'secretkry'

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        if request.form["form_button_1"]:
            return render_template('index.html', test_id="pressed form nr 1")
        if request.form["form_button_2"]:
            return render_template('index.html', test_id="pressed form nr 2")

    return render_template('index.html', test_id="hasnt yet pressed form button")

if __name__ == '__main__':
    app.run(port=44444)

这是我的index.html 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form action="#" method="post">
    <button name="form_button_1" type="submit" value="random_value1">form button 1</button>
</form>

<br>

<form action="#" method="post">
    <button name="form_button_2" type="submit" value="random_value2">form button 2</button>
</form>

<p>{{test_id}}</p>

</body>
</html>

当我运行此命令时,我收到错误:400 错误请求。当我在 index.html 中只有 1 个表单时,不会发生这种情况 (index.html 看起来像这样)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form action="#" method="post">
    <button name="form_button_1" type="submit" value="random_value1">form button 1</button>
</form>

<br>

<p>{{test_id}}</p>

</body>
</html>

包含flask的python文件看起来像这样

from flask import Flask, request, render_template

app = Flask(__name__)
app.secret_key = 'secretkry'

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        if request.form["form_button_1"]:
            return render_template('index.html', test_id="pressed form nr 1")

    return render_template('index.html', test_id="hasnt yet pressed form button")

if __name__ == '__main__':
    app.run(port=44444)

同样,第一个示例不起作用,但第二个示例(只有 1 个表单)起作用。 我想知道当存在 2 个表单时如何提交单击的表单。我使用的是 Kali Linux,如果这有什么不同的话。预先感谢

python html flask
1个回答
0
投票

request.form
字典将仅包含您提交的表单中的按钮。当您尝试访问其他按钮时,您会收到错误消息。您需要检查名称是否在字典中,而不是字典元素的值:

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        if "form_button_1" in request.form:
            return render_template('index.html', test_id="pressed form nr 1")
        elif "form_button_2" in request.form:
            return render_template('index.html', test_id="pressed form nr 2")

    return render_template('index.html', test_id="hasnt yet pressed form button")

另一种选择是为按钮指定相同的名称,并检查值:

<form action="#" method="post">
    <button name="form_button" type="submit" value="random_value1">form button 1</button>
</form>

<br>

<form action="#" method="post">
    <button name="form_button" type="submit" value="random_value2">form button 2</button>
</form>

然后

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        if request.form["form_button"] == "random_value1":
            return render_template('index.html', test_id="pressed form nr 1")
        elif request.form["form_button"] == "random_value2":
            return render_template('index.html', test_id="pressed form nr 2")

    return render_template('index.html', test_id="hasnt yet pressed form button")
© www.soinside.com 2019 - 2024. All rights reserved.