Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask form post call - request.form is an empty dict

Tags:

flask

Form login.html:

<form method="POST">
    <input type="text" placeholder="username" required>
    <input type="password" placeholder="password" required>
    <button type="submit">Sign in</button>
</form>

app.py:

@app.route("/", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        print(request.form)    # prints empty dict
    return render_template("login.html")
like image 283
Prashanth Avatar asked Oct 20 '25 01:10

Prashanth


1 Answers

The name parameter of inputs in the form is needed in any html form for it to be POSTed, otherwise they are left unprocessed.

So the right html would be login.html:

<form method="POST">
    <input type="text" placeholder="username" name="username" required>
    <input type="password" placeholder="password" name="password" required>
    <button type="submit">Sign in</button>
</form>

Now flask should be able to dict up the input elements by name and value.

like image 179
Prashanth Avatar answered Oct 25 '25 09:10

Prashanth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!