Request.files 400 Bad Request in flask
up vote
0
down vote
favorite
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>
flask upload
add a comment |
up vote
0
down vote
favorite
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>
flask upload
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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>
flask upload
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>
flask upload
flask upload
asked Nov 10 at 15:01
Berat Bozkurt
33
33
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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['gelinFoto'] if request.files.get('gelinFoto') else None
damatFoto = request.files['damatFoto'] if request.files.get('damatFoto') else 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 dfilename and gfilename:
kisi = bilgi(gelinFoto = gfilename, damatFoto = dfilename)
elif gfilename:
kisi = bilgi(gelinFoto = gfilename)
elif dfilename:
kisi = bilgi(damatFoto = dfilename)
unfortunately, the problem has not changed. I get the same mistake.
– Berat Bozkurt
Nov 10 at 22:39
@BeratBozkurt provide more information, like the whole Traceback message
– DarkSuniuM
Nov 11 at 0:23
imgur.com/a/N1TeVJ3
– Berat Bozkurt
Nov 12 at 7:34
@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?
– DarkSuniuM
Nov 12 at 10:20
Request' object has no attribute 'get' (i.imgsafe.org/dc/dc12dca9f7.png)
– Berat Bozkurt
11 hours ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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['gelinFoto'] if request.files.get('gelinFoto') else None
damatFoto = request.files['damatFoto'] if request.files.get('damatFoto') else 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 dfilename and gfilename:
kisi = bilgi(gelinFoto = gfilename, damatFoto = dfilename)
elif gfilename:
kisi = bilgi(gelinFoto = gfilename)
elif dfilename:
kisi = bilgi(damatFoto = dfilename)
unfortunately, the problem has not changed. I get the same mistake.
– Berat Bozkurt
Nov 10 at 22:39
@BeratBozkurt provide more information, like the whole Traceback message
– DarkSuniuM
Nov 11 at 0:23
imgur.com/a/N1TeVJ3
– Berat Bozkurt
Nov 12 at 7:34
@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?
– DarkSuniuM
Nov 12 at 10:20
Request' object has no attribute 'get' (i.imgsafe.org/dc/dc12dca9f7.png)
– Berat Bozkurt
11 hours ago
|
show 1 more comment
up vote
0
down vote
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['gelinFoto'] if request.files.get('gelinFoto') else None
damatFoto = request.files['damatFoto'] if request.files.get('damatFoto') else 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 dfilename and gfilename:
kisi = bilgi(gelinFoto = gfilename, damatFoto = dfilename)
elif gfilename:
kisi = bilgi(gelinFoto = gfilename)
elif dfilename:
kisi = bilgi(damatFoto = dfilename)
unfortunately, the problem has not changed. I get the same mistake.
– Berat Bozkurt
Nov 10 at 22:39
@BeratBozkurt provide more information, like the whole Traceback message
– DarkSuniuM
Nov 11 at 0:23
imgur.com/a/N1TeVJ3
– Berat Bozkurt
Nov 12 at 7:34
@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?
– DarkSuniuM
Nov 12 at 10:20
Request' object has no attribute 'get' (i.imgsafe.org/dc/dc12dca9f7.png)
– Berat Bozkurt
11 hours ago
|
show 1 more comment
up vote
0
down vote
up vote
0
down vote
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['gelinFoto'] if request.files.get('gelinFoto') else None
damatFoto = request.files['damatFoto'] if request.files.get('damatFoto') else 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 dfilename and gfilename:
kisi = bilgi(gelinFoto = gfilename, damatFoto = dfilename)
elif gfilename:
kisi = bilgi(gelinFoto = gfilename)
elif dfilename:
kisi = bilgi(damatFoto = dfilename)
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['gelinFoto'] if request.files.get('gelinFoto') else None
damatFoto = request.files['damatFoto'] if request.files.get('damatFoto') else 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 dfilename and gfilename:
kisi = bilgi(gelinFoto = gfilename, damatFoto = dfilename)
elif gfilename:
kisi = bilgi(gelinFoto = gfilename)
elif dfilename:
kisi = bilgi(damatFoto = dfilename)
edited 6 hours ago
answered Nov 10 at 17:47
DarkSuniuM
776819
776819
unfortunately, the problem has not changed. I get the same mistake.
– Berat Bozkurt
Nov 10 at 22:39
@BeratBozkurt provide more information, like the whole Traceback message
– DarkSuniuM
Nov 11 at 0:23
imgur.com/a/N1TeVJ3
– Berat Bozkurt
Nov 12 at 7:34
@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?
– DarkSuniuM
Nov 12 at 10:20
Request' object has no attribute 'get' (i.imgsafe.org/dc/dc12dca9f7.png)
– Berat Bozkurt
11 hours ago
|
show 1 more comment
unfortunately, the problem has not changed. I get the same mistake.
– Berat Bozkurt
Nov 10 at 22:39
@BeratBozkurt provide more information, like the whole Traceback message
– DarkSuniuM
Nov 11 at 0:23
imgur.com/a/N1TeVJ3
– Berat Bozkurt
Nov 12 at 7:34
@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?
– DarkSuniuM
Nov 12 at 10:20
Request' object has no attribute 'get' (i.imgsafe.org/dc/dc12dca9f7.png)
– Berat Bozkurt
11 hours ago
unfortunately, the problem has not changed. I get the same mistake.
– Berat Bozkurt
Nov 10 at 22:39
unfortunately, the problem has not changed. I get the same mistake.
– Berat Bozkurt
Nov 10 at 22:39
@BeratBozkurt provide more information, like the whole Traceback message
– DarkSuniuM
Nov 11 at 0:23
@BeratBozkurt provide more information, like the whole Traceback message
– DarkSuniuM
Nov 11 at 0:23
imgur.com/a/N1TeVJ3
– Berat Bozkurt
Nov 12 at 7:34
imgur.com/a/N1TeVJ3
– Berat Bozkurt
Nov 12 at 7:34
@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?
– DarkSuniuM
Nov 12 at 10:20
@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?
– DarkSuniuM
Nov 12 at 10:20
Request' object has no attribute 'get' (i.imgsafe.org/dc/dc12dca9f7.png)
– Berat Bozkurt
11 hours ago
Request' object has no attribute 'get' (i.imgsafe.org/dc/dc12dca9f7.png)
– Berat Bozkurt
11 hours ago
|
show 1 more comment
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240205%2frequest-files-400-bad-request-in-flask%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown