我正在try 上传文件&使用flask将数据任务输入我的MongoDB

Bad Request

我的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>

&amp;我的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)

推荐答案

这里的错误是由于访问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的实例.

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

Mongodb相关问答推荐

如何拉平mongo DB中的对象并在根目录中保存时有条件地重命名字段?

在服务器上部署后端时判断??=默认判断

为什么 mongoose 在 mongodb 中找不到我的数据

mongoDB中数组中的聚合和元素

从具有多个数组匹配 MongoDB 的两个集合中采样数据

如何获取mongodb ObjectId的值?

Mongo:查找具有 0 个关联文档的文档,成本更低

MongoDB:嵌套数组计数+原始文档

在 Mongo 聚合中,可以通过分组生成 3 个不同的计数

Ruby on Rails 的 Cassandra、mongodb 或 couchdb

ExpressJS & Mongoose REST API struct :最佳实践?

Spring Mongo 条件查询两次相同的字段

MongoDB 中的所有列

MongoDB - 我如何找到另一个集合中的文档未引用的所有文档

Mongodb 类型引用 node

遍历所有 Mongo 数据库

在 mongodb 中插入当前日期时间

如何从 Mongoose 模型对象中获取集合名称

MongoDB MapReduce - 发出一个键/一个值不调用reduce

是否可以使用聚合框架对 MongoDB 中的 2 个字段求和?