• Register
0 votes
320 views

Problem :

I am trying to do the file upload & enter the data task into my MongoDB using a flask but I had below error when I filled my form & upload my image:

“Bad Request The browser (or proxy) sent a request that this server could not understand.”

 My Python Code:

@app.route('/', methods=['GET', 'POST'])
def index():
   mytarget = os.path.join(app_root, 'static/img/')
    if not os.path.isdir(mytarget):
        os.mkdir(mytarget)
    if request.method == 'POST':
        myname = request.form['u_name']
        mypassword = request.form['u_pass']
        myemail = request.form['u_email']
        file_name = ''
        for file in request.form['u_img']:
            file_name = file.filename
            mydestination = '/'.join([mytarget, file_name])
            file.save(mydestination)
        mongo.db.employee_entry.insert({'name': myname, 'password': mypassword, 'email': myemail, 'img_name': file_name})
        return render_template('index.html')
    else:
        return render_template('index.html')

 

7 5 2
3,870 points

1 Answer

0 votes

Solution :

Your error is resulting from the BadRequestKeyError because of access to the key that doesn't exist in your request.form.

ipdb> request.form['u_img']
*** BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

Uploaded files are keyed under the request.files and not the request.form dictionary. Also, you need to use the loop as the value keyed under u_img is the instance of FileStorage and not at all iterable.

Please refer below updated code that will help you in resolving all of the issues.

@app.route('/', methods=['GET', 'POST'])
def index():
    mytarget = os.path.join(app_root, 'static/img/')
    if not os.path.isdir(mytarget):
        os.makedirs(mytarget)
    if request.method == 'POST':
        ...
        file = request.files['u_img']
        file_name = file.myfilename or ''
        mydestination = '/'.join([mytarget, file_name])
        file.save(mydestination)
        ...
    return render_template('index.html')

 

9 7 4
38,600 points

Related questions

0 votes
0 answers 8 views
8 views
I have a dataframe of almost 120000 records as follows. Also I have a mongoDB collection which looks exacly same as below dataframe ItemID ParentID ItemRating ItemPrice Qty A1 ItemA1 0 12 100 A2 ItemA2 0 15 200 B1 ItemB1 0 20 300 B2 ItemB2 0 25 400 B3 ItemB3 0 30 ... PyMongo update_many method by setting upsert=true. but I am not sure how can I do that ? how should I write my filter condition ?
asked Sep 13 NguyenTram 1k points
1 vote
1 answer 555 views
555 views
Problem: I installed flask in my system by running the command: pip install flask Collecting flask Downloading https://files.pythonhosted.org/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94kB) |████████████████████████████████| ... operable program or batch file. Whats the problem running this can any one tell me? Thanks in advance.
asked May 12 samhaz 5k points
1 vote
1 answer 180 views
180 views
Problem : I am getting bellow flask app error error: could not locate flask application. you did not provide the flask_app environment variable.
asked Nov 8, 2019 peterlaw 6.9k points
0 votes
1 answer 47 views
0 votes
1 answer 595 views
595 views
Problem : I am trying to deploy my Python Flask application in my Azure web app. I had created web app(Flask) and published the code. After publishing it, I am facing below error from the site. The page cannot be displayed because an internal server error ... . But this was the case only in my subscription(my free subscription with MSDN). But it was working fine in the Organisation subscription.
asked Dec 16, 2019 alecxe 7.5k points