首页 > 解决方案 > Request.files 400 烧瓶中的错误请求

问题描述

我的项目中有一个项目的烧瓶我正在上传我正在开发的应用程序。我有 2 个图片上传区域。加载 2 时我没有遇到任何问题。 400 Bad Request: KeyError: 当我上传图像或按下发送按钮时,我在“gelinFoto”样式中出现错误,而我从未加载它。我在哪里犯错?

def admin():
form = KisiForm(request.form)
    if request.method == "POST":
        gelinFoto = request.files['gelinFoto']
        damatFoto = request.files['damatFoto']

        if gelinFoto or damatFoto:
            yol = app.config['UPLOAD_FOLDER'] + whuser
            yol = yol + '/profil'
            gfilename = secure_filename(gelinFoto.filename)
            dfilename = secure_filename(damatFoto.filename)
            gelinFoto.save(os.path.join(yol, gfilename))
            damatFoto.save(os.path.join(yol, dfilename))

        kisi = bilgi(gelinFoto = gfilename, damatFoto = dfilename)
        db.session.add(kisi)
        db.session.commit()
        return redirect(url_for("admin"))
    return render_template("admin/index.html",form=form)

html

<form method="post" enctype="multipart/form-data" class="col-12">
<div class="form-group">
 <label for="exampleFormControlFile1">Gelinin Fotoğrafı : </label>
  <div class="upload">
    <img src="{{ url_for('static', filename='admin/images/upload.png') }}" class="uploadImage" alt="">{{ render_field(form.gelinFoto,id="gelinFoto",class="gdfoto",accept=".png,.jpg,.jpeg") }} </div>
    <small id="emailHelp" class="form-text text-muted">Gelinin fotoğrafını yükleyiniz.</small>
 </div>
<div class="form-group">
     <label for="exampleFormControlFile1">Damatın Fotoğrafı : </label>
     <div class="upload"><img src="{{ url_for('static', filename='admin/images/upload.png') }}" class="uploadImage" alt="">{{ render_field(form.damatFoto,id="damatFoto",class="gdfoto",accept=".png,.jpg,.jpeg") }}
  </div>
  <small id="emailHelp" class="form-text text-muted">Damatın fotoğrafını yükleyiniz.</small>

标签: flaskupload

解决方案


在你看来,你两者都有

gelinFoto = request.files['gelinFoto']
damatFoto = request.files['damatFoto']

这就是您收到该错误的原因

如果没有提供文件,则没有request.files['gelinFoto']example,Python 会尝试查找它,但不能导致没有名为gelinFoto!的键。

最简单的技巧是这样定义主题:

gelinFoto = request.files.get('gelinFoto', None)
damatFoto = request.files.get('damatFoto', None)

这样,它使用内联条件来获取键,如果未提供键,则设置值None

稍后在您的代码中,我看到您又这样做了,

if gelinFoto or damatFoto:
    # ... Your other coders 
    gfilename = secure_filename(gelinFoto.filename)
    dfilename = secure_filename(damatFoto.filename)
    gelinFoto.save(os.path.join(yol, gfilename))
    damatFoto.save(os.path.join(yol, dfilename))

这是错误的,你正在检查它,or然后你除了两者都不是None

最好分别对每一项进行操作,例如:

if gelinFoto:
    yol = app.config['UPLOAD_FOLDER'] + whuser
    yol = yol + '/profil'
    gfilename = secure_filename(gelinFoto.filename)
    gelinFoto.save(os.path.join(yol, gfilename)

if damatFoto:
    yol = app.config['UPLOAD_FOLDER'] + whuser
    yol = yol + '/profil'
    dfilename = secure_filename(damatFoto.filename)
    damatFoto.save(os.path.join(yol, dfilename)

# I'm not sure if there is a better way to do this but about kisi line This is the best that came up to me ( ofcourse there are better ways )
if gelinFoto and damatFoto:
    kisi = bilgi(gelinFoto = gfilename, damatFoto = dfilename)
elif gelinFoto:
    kisi = bilgi(gelinFoto = gfilename)
elif damatFoto:
    kisi = bilgi(damatFoto = dfilename)

推荐阅读