Flask - Bad Request 浏览器(或代理)发送了一个该服务器无法理解的请求 [英] Flask - Bad Request The browser (or proxy) sent a request that this server could not understand
问题描述
我正在尝试上传文件 &使用烧瓶将数据任务输入到我的 MongoDB 中但是当我填写表格时出现了这个错误 &上传图片:
I'm trying to do a file upload & enter data task into my MongoDB using flask but I had this error when I filled my form & upload the image:
错误请求浏览器(或代理)发送了此服务器无法理解的请求.
Bad Request The browser (or proxy) sent a request that this server could not understand.
我的 HTML 代码
<form class="form-check form-control" method="post" enctype="multipart/form-data" action="{{ url_for('index') }}">
<label>Full Name*</label></td>
<input name="u_name" type="text" class="text-info my-input" required="required" />
<label>Email*</label>
<input name="u_email" type="email" class="text-info my-input" required="required" />
<label>Password*</label>
<input name="u_pass" type="password" class="text-info my-input" required="required" />
<label>Your Image*</label>
<input name="u_img" type="file" class="text-info" required="required" /></td>
<input name="btn_submit" type="submit" class="btn-info" />
</form>
&我的python代码:
& my python code:
from flask import Flask, render_template, request, url_for
from flask_pymongo import PyMongo
import os
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'flask_assignment'
app.config['MONGO_URI'] = 'mongodb://<user>:<pass>@<host>:<port>/<database>'
mongo = PyMongo(app)
app_root = os.path.dirname(os.path.abspath(__file__))
@app.route('/', methods=['GET', 'POST'])
def index():
target = os.path.join(app_root, 'static/img/')
if not os.path.isdir(target):
os.mkdir(target)
if request.method == 'POST':
name = request.form['u_name']
password = request.form['u_pass']
email = request.form['u_email']
file_name = ''
for file in request.form['u_img']:
file_name = file.filename
destination = '/'.join([target, file_name])
file.save(destination)
mongo.db.employee_entry.insert({'name': name, 'password': password, 'email': email, 'img_name': file_name})
return render_template('index.html')
else:
return render_template('index.html')
app.run(debug=True)
推荐答案
由于访问了一个在 request.form 中不存在的键而导致的
BadRequestKeyError
错误代码>.
The error there is resulting from a BadRequestKeyError
because of access to a key that doesn't exist in request.form
.
ipdb> request.form['u_img']
*** BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
上传的文件在 request.files
而不是 request.form
字典下键入.此外,您需要丢失循环,因为 u_img
下键入的值是 FileStorage
的实例,而不是 iterable.
Uploaded files are keyed under request.files
and not request.form
dictionary. Also, you need to lose the loop because the value keyed under u_img
is an instance of FileStorage
and not iterable.
@app.route('/', methods=['GET', 'POST'])
def index():
target = os.path.join(app_root, 'static/img/')
if not os.path.isdir(target):
os.makedirs(target)
if request.method == 'POST':
...
file = request.files['u_img']
file_name = file.filename or ''
destination = '/'.join([target, file_name])
file.save(destination)
...
return render_template('index.html')
这篇关于Flask - Bad Request 浏览器(或代理)发送了一个该服务器无法理解的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!