• Register
0 votes
403 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

Please log in or register to answer this question.

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 29 views
29 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, 2020 NguyenTram 1k points
1 vote
1 answer 1.3K views
1.3K 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, 2020 samhaz 5k points
1 vote
1 answer 253 views
253 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 140 views
0 votes
1 answer 913 views
913 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
0 votes
1 answer 273 views
273 views
Problem: I am very new to php, mongodb and codeigniter. At this moment I am trying to connect the mongodb with the php codeigniter framework. I have already made changes in “php.in file extension=php_mongodb.dll.” I have also put that file in xampp/php/ext/ ... to MongoDB? Please Note: Currently I am using the php 7, xampp and mongo db is installed in my machine the 3.4 version.
asked Sep 3, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 23 views
23 views
I am struggling with Jetson TX2 board (aarch64). I need to install python wrapper for OpenCV. I can do: $ sudo apt-get install python-opencv But I cannot do: $ sudo pip install opencv-python Is this because there is no proper wheel file in http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv ? Is there a way to install opencv-python through pip?
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 132 views
132 views
Problem : I want to intstall the TensorFlow using pip. I tried below command: $ pip install tensorflow –user But I got below error: Collecting tensorflow Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow How can I install the TensorFlow?
asked Feb 19, 2020 mphil 2.3k points
0 votes
1 answer 138 views
138 views
Problem : When I try to install PyGame with:pip install pygame i am getting below error python could not find a version that satisfies the requirement
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 143 views
143 views
Problem : I am trying to run my registration jsp page with the servlet. But it is throwing following error: The server encountered an internal error that prevented it from fulfilling this request. java.lang.NullPointerException com.java.task11.utils.ValidateUtil.EmailVal( ... .tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:62) I am unable to figure out why this is happening? Any clues?
asked Dec 2, 2019 alecxe 7.5k points