英文:
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('/',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>
I am not sure why it is not detecting imagefile... Does anyone know how to fix this?
答案1
得分: 0
你没有正确设置你的单一路由的GET
和POST
方法。此外,你还需要在你的路由上添加一些逻辑来处理来自表单的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('/', 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)
I also noticed that you have a typo in your form method
attribute.
<form class="p-3 text-center" action='/' method="POST" enctype="multipart/form-data">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论