Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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
Flask Request.400烧瓶中的错误请求_Flask_Upload - Fatal编程技术网

Flask Request.400烧瓶中的错误请求

Flask Request.400烧瓶中的错误请求,flask,upload,Flask,Upload,在我的项目中有一个项目,我正在上传一个我正在开发的应用程序。我有2个图像上传区域。我加载2时没有任何问题。400错误请求:KeyError:当我上传图像时,或当我从未加载图像时按下发送按钮时,我在“gelinFoto”样式中遇到错误。我哪里出错了 def admin(): form = KisiForm(request.form) if request.method == "POST": gelinFoto = request.files['gelinFoto']

在我的项目中有一个项目,我正在上传一个我正在开发的应用程序。我有2个图像上传区域。我加载2时没有任何问题。400错误请求: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


格里宁·福托拉夫:
{{render_字段(form.gelinfo,id=“gelinfo”,class=“gdfoto”,accept=“.png,.jpg,.jpeg”)}
格列宁·福托拉夫·恩努伊伊尼茨(Gelinin fotoğrafınıyükleyiniz)。
Damatın Fotoğrafı
{{render_字段(form.damatFoto,id=“damatFoto”,class=“gdfoto”,accept=“.png,.jpg,.jpeg”)}
Damatın fotoğrafınıyükleyiniz。

在你看来,两者都有

gelinFoto = request.files['gelinFoto']
damatFoto = request.files['damatFoto']
这就是为什么你会犯这个错误

如果没有提供该文件,那么就没有
请求。例如,files['gelinfo']
,Python会尝试查找它,但这不会导致没有名为
gelinfo
的键

最简单的技巧是定义如下主题:

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))
这是错误的,您正在用
检查它,然后用u检查,除非两者都不是
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)

不幸的是,这个问题没有改变。我也犯了同样的错误。@berabozkurt提供了更多信息,比如整个回溯message@BeratBozkurt将这些数据作为文本/代码添加到您的问题中,顺便说一句,您没有更改我说的内容,您希望错误如何消失?@berabozkurt抱歉,这是一个小错误,更新了答案,
request.get
更改为
request.files.get
,这应该可以正常工作。还有一个错误:)我在if dfilename和gfilename中的赋值错误之前引用了一个局部变量“dfilename”:
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)