Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask-错误请求浏览器(或代理)发送了此服务器无法理解的请求_Python_Mongodb_Flask_Pymongo - Fatal编程技术网

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

Python Flask-错误请求浏览器(或代理)发送了此服务器无法理解的请求,python,mongodb,flask,pymongo,Python,Mongodb,Flask,Pymongo,我正在尝试使用flask进行文件上传并将数据任务输入我的MongoDB 但我在填写表格并上传图像时出现了以下错误: 错误请求 浏览器(或代理)发送了此服务器无法理解的请求 我的HTML代码 <form class="form-check form-control" method="post" enctype="multipart/form-data" action="{{ url_for('index') }}"> <label>F

我正在尝试使用flask进行文件上传并将数据任务输入我的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)
从flask导入flask,为
从比蒙戈进口比蒙戈
导入操作系统
app=烧瓶(名称)
app.config['MONGO\u DBNAME']='flask\u赋值'
app.config['MONGO_URI']='mongodb://:@://'
mongo=PyMongo(应用程序)
app_root=os.path.dirname(os.path.abspath(_文件__))
@app.route('/',方法=['GET','POST'])
def index():
target=os.path.join(app_root,“static/img/”)
如果不是os.path.isdir(目标):
os.mkdir(目标)
如果request.method==“POST”:
name=请求。表单['u_name']
密码=请求。表单['u_pass']
电子邮件=请求。表格['u_email']
文件名=“”
对于request.form['u_img']中的文件:
file\u name=file.filename
destination='/'.join([目标,文件名])
文件保存(目标)
employee_entry.insert({'name':name,'password':password,'email':email,'img_name':file_name})
返回渲染模板('index.html')
其他:
返回渲染模板('index.html')
app.run(debug=True)

由于访问
请求表单中不存在的密钥而导致的
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\u img
下键入的值是
FileStorage
的实例,不可编辑


谢谢你的回答解决了我的问题。我的html输入文本没有属性“name”。在那个地方之后,一切都很顺利。下面是工作代码:在我的例子中,输入标记的name属性的值在HTML中是不同的,并且我试图在f=request.files['file']中访问的键是不同的。将两者都改为“文件”,它就起作用了。
@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')