Flask 的 BadRequestKeyError

我已经查看了有关此错误的所有其他问题,但要么解决方案不起作用,要么我已经按照解决方案所说的去做了。我的 app.py 代码:


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

def index():

    if request.method == "POST":

        article_content = request.form["article_content"]

        threshold_val = request.form["threshold_input"]

        predictionCount = request.form["prediction_count"]

HTML 代码:


<div>

    <div>

      <form action="/" method="POST">

        <input class="Input-Box" type="text" name="article_content" />

        <br />

        <br />

        <input type="submit" value="Find Systems" />

      </form>

    </div>

    <div class="Settings">

      <div style="align-items: center; display: inline-block;">

        <h6>Threshold Value(0-1.0):</h6>

        <form action="/" method="POST">

          <input type="text" name="threshold_input" />

        </form>

      </div>

      <div style="align-items: center; display: inline-block;">

        <h6>Number of predictions(1-24):</h6>

        <form action="/" method="POST">

          <input type="text" name="prediction_count" />

        </form>

      </div>

    </div>

  </div>

</div>

有趣的是,Flask 从“article_content”获取输入没有问题,但会匹配“threshold_input”和“prediction_count”。我检查了 POST 方法,将 HTML 表单设置为 POST 方法,并确保 HTML 元素的名称和 request.form 函数中的文本相同。任何帮助,将不胜感激。追溯如下:


Traceback (most recent call last)


    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__


    return self.wsgi_app(environ, start_response)


    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app


    response = self.handle_exception(e)


    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception



临摹微笑
浏览 155回答 1
1回答

浮云间

您需要将所有表单控件放在一个form标签下,并在submit其中添加一个输入。您现在使用的提交输入仅发送其封闭form标签内的输入,而不发送其他form标签内的输入。https://www.w3.org/TR/html4/interact/forms.html#submit-format<form action="/" method="POST">&nbsp; <input class="Input-Box" type="text" name="article_content" />&nbsp; <input type="text" name="threshold_input" />&nbsp; <input type="text" name="prediction_count" />&nbsp; <br />&nbsp; <br />&nbsp; <!-- This input submits everything inside of this form tag -->&nbsp; <input type="submit" value="Find Systems" /></form>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python