Reputation: 1322
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
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
Reputation: 1322
The name
parameter of inputs in the form is needed in any html form for it to be POST
ed, 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