werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: 我无法确定为什么出现此错误

huangapple go评论69阅读模式
英文:

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: I am not able to figure why I am getting this error

问题

以下是您提供的内容的翻译:

我正在请求上传文件,但我收到一个关键错误,尽管我在表单中有name="imagefile"。

from flask import Flask, render_template, request

app = Flask(__name__)

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

@app.route('/', methods=['POST'])
def predict():
    imagefile = request.files['imagefile']
    image_path = "./images/" + imagefile.filename
    imagefile.save(image_path)

    return render_template('index.html')

if __name__ == '__main__':
    app.run(port=3000, debug=True)
<!DOCTYPE html>
<html>
    <head>
        <title>Tutorial</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    </head>

    <body>
        <h1>Image Classifer</h1>

        <form class="p-3 text-center" action='/' method="post" enctype="multipart/form-data"> 
            <input class="form-control" type="file" name='imagefile' >
            <input class="btn btn-primary mt-3" type="submit" value="Predict Image" >
        </form>
    </body>
</html>

我不确定为什么它没有检测到imagefile... 有人知道如何修复这个问题吗?

英文:

I am requesting a file to be uploaded but I get a keyerror even though I have name="imagefile" in my form.

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route(&#39;/&#39;,methods=[&#39;GET&#39;])


@app.route(&#39;/&#39;, methods=[&quot;POST&quot;])
def predict():
    imagefile = request.files[&#39;imagefile&#39;]
    image_path = &quot;./images/&quot;+imagefile.filename
    imagefile.save(image_path)

    return render_template(&#39;index.html&#39;)

if __name__ == &#39;__main__&#39;:
    app.run(port=3000, debug=True)


&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Tutorial&lt;/title&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T&quot; crossorigin=&quot;anonymous&quot;&gt;
    &lt;/head&gt;

    &lt;body&gt;
        &lt;h1&gt;Image Classifer&lt;/h1&gt;

        &lt;form class=&quot;p-3 text-center&quot; action=&#39;/&#39;, method=&quot;&quot;post enctype=&quot;multipart/form-data&quot;&gt; 
            &lt;input class=&quot;form-control&quot; type=&quot;file&quot; name=&#39;imagefile&#39; &gt;
            &lt;input class=&quot;btn btn-primary mt-3&quot; type=&quot;submit&quot; value=&quot;Predict Image&quot; &gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;

I am not sure why it is not detecting imagefile... Does anyone know how to fix this?

答案1

得分: 0

你没有正确设置你的单一路由的GETPOST方法。此外,你还需要在你的路由上添加一些逻辑来处理来自表单的POST请求。

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/', methods=["GET", "POST"])
def predict():
    if request.method == "POST":
        imagefile = request.files['imagefile']
        image_path = "./images/" + imagefile.filename
        imagefile.save(image_path)

    return render_template('index.html')

if __name__ == '__main__':
    app.run(port=3000, debug=True)

我还注意到你的表单method属性中有一个拼写错误。

<form class="p-3 text-center" action='/' method="POST" enctype="multipart/form-data">
英文:

You aren't correctly setting your GET and POST methods for your single route. In addition to this, you also need some logic on your route to handle the POST request from your form.

from flask import Flask, render_template, request

app = Flask(__name__)


@app.route(&#39;/&#39;, methods=[&quot;GET&quot;, &quot;POST&quot;])
def predict():
    if request.method == &quot;POST&quot;:
        imagefile = request.files[&#39;imagefile&#39;]
        image_path = &quot;./images/&quot; + imagefile.filename
        imagefile.save(image_path)

    return render_template(&#39;index.html&#39;)


if __name__ == &#39;__main__&#39;:
    app.run(port=3000, debug=True)

I also noticed that you have a typo in your form method attribute.

&lt;form class=&quot;p-3 text-center&quot; action=&#39;/&#39; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;

huangapple
  • 本文由 发表于 2023年3月1日 10:07:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定