2

flask with a project in my project I'm uploading an application I'm developing. I have 2 image upload areas. I don't have any trouble when I load 2. 400 Bad Request: KeyError: I get an error in the 'gelinFoto' style when I upload an image or when I push the send button when I never load it. Where am I making a mistake?

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> 

1 Answer 1

4

In your view you have both

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

This is why you get that error

When the file is not provided, then there is no request.files['gelinFoto'] for example, and Python tries to look it up, but it can't cause there is no key named gelinFoto!

The simplest trick is defining theme like this:

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

This way it uses an inline condition to get the keys, if they are not provided, then it sets the value None

Later in your code I see you did that again,

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

It's wrong, you are checking with it with or and then u except both to be not None !!

It's better to do the operation for each one separately, like:

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) 
Sign up to request clarification or add additional context in comments.

8 Comments

unfortunately, the problem has not changed. I get the same mistake.
@BeratBozkurt provide more information, like the whole Traceback message
@BeratBozkurt Add this data as text/code to your question, by the way, u didn't changed what I said, How u expect the error to be gone?
@BeratBozkurt Sorry, it was a little mistake, updated the answer, request.get changed to request.files.get, this should work fine
one more mistake :) I get a local variable 'dfilename' referenced before assignment error in if dfilename and gfilename:
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.