Prashanth
Prashanth

Reputation: 1322

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

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")

Upvotes: 4

Views: 4912

Answers (2)

G.Darcan
G.Darcan

Reputation: 38

It returns empty dict because you don't specify the name of the object you requested.You should specify name from the html :

<input type="text" placeholder="username" name="username" required>

and in Python use something like :

request.form['username'] or request.form.get('username')

instead of : (request.form)

Also a topic to have a look at : How to get data received in Flask request

Upvotes: 0

Prashanth
Prashanth

Reputation: 1322

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.

Upvotes: 4

Related Questions