Flask receiving empty forms

22,048

Solution 1

I had this problem, but it was because I forgot to assign a name attribute to my input elements and I was trying to refer access the form data by the id attribute instead

i.e.

My HTML and Python was as shown below

HTML

<input type="text" id="usernameTxtBx">

Python

request.form['usernameTxtBx']

What I have done now:

HTML

<input type="text" name="username" id="usernameTxtBx">

Python

request.form['username']

I also needed to ensure that I was using a POST request. A GET request gave me an empty dictionary in my python code.

The OP made neither of these mistakes. But this may help someone that stumbles on this thread.

Solution 2

I had that problem. Some tools like postman or some libraries or web browser sends the data in a way that flask does not identify as posted values. From my point of view this is a flask issue.

This is the workaround I followed to solve it: 1 - I sent the information using json. Have a look to this:

How to send a JSON object using html form data

2 - I instead of getting the parameters using: value = request.form["myparamname"] I used this:

json_data = request.get_json(force=True) 

value = json_data["myparamname"]
Share:
22,048

Related videos on Youtube

semisight
Author by

semisight

CS Student at USC.

Updated on November 06, 2020

Comments

  • semisight
    semisight over 3 years

    I'm trying to get a simple form set up in Flask for my own education. I've got a login.html page with this form code:

    <form action="{{ url_for('login') }}" method="post">
        <div>
            <label for="username">Username</label>
            <div>
                <input type="text" id="username" name="username" placeholder="Username">
            </div>
        </div>
        <div>
            <label for="password">Password</label>
            <div>
                <input type="password" id="password" name="password" placeholder="Password">
            </div>
        </div>
        <div >
            <input class="btn" type="submit">
        </div>
    </form>
    

    I'm using code like the following to receive it, but Flask returns an empty request.form so I can't process it.

    @app.route('/login', methods=['GET', 'POST'])
    def login():
        if request.method == 'POST':
            request.form['username']
            ...
    

    I really don't want to learn another library (WTForms) right now, and I'm using bootstrap so that will add to the headache. What am I not seeing here with Flask/HTML?

    • Ignas Butėnas
      Ignas Butėnas over 11 years
      does the request reaches your view function? Maybe form has different URL in action not "/login"? Try to put just "." in action. Just interested if it really lands in the view as you expect.
  • avloss
    avloss about 7 years
    id="usernameTxtBx" is unnecessary, as it's not reflected in the request. You can access GET and POST variables together though request.args instead of request.form which only accesses POST
  • Dave C
    Dave C over 3 years
    Thank you! WTForms seems great, but is simply overkill for my needs.