【问题标题】:Flask - Bad Request The browser (or proxy) sent a request that this server could not understand [duplicate]Flask - 错误请求浏览器(或代理)发送了该服务器无法理解的请求[重复]
【发布时间】:2018-07-24 14:54:20
【问题描述】:

我正在尝试使用烧瓶进行文件上传并将数据任务输入到我的 MongoDB 但是当我填写表格并上传图片时出现此错误:

错误请求 浏览器(或代理)发送了此服务器无法理解的请求。

我的 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代码:

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)

【问题讨论】:

    标签: python mongodb flask pymongo


    【解决方案1】:

    由于访问request.form 中不存在的密钥,BadRequestKeyError 导致错误。

    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 的一个实例,而不是可迭代

    @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')
    

    【讨论】:

    • 谢谢你的回答解决了我的问题。我的 html 输入文本没有属性“名称”。之后一切正常。这是工作代码:
    • 在我的例子中,输入标签的 name 属性值在 HTML 中是不同的,我试图在 f = request.files['file'] 中访问的键不同。将两者都更改为“文件”并且有效。
    猜你喜欢
    • 2018-12-13
    • 2021-07-04
    • 2018-06-21
    • 1970-01-01
    • 2018-12-20
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    相关资源
    最近更新 更多