I don't know how to check if user fill field in form in flask.
I have this form
<form method="POST" class="form-horizontal">
<input type="text" name="name" value="{{ name }}">
<input type="submit" class="btn" value="Add"/>
</form>
then I want check value in field and process it.
if request.method == 'POST':
name = request.form['name']
if name is None: // this doesn't work
# do something
if name == string.empty: // this also doesn't work
# do something
Please could you give me some advise. Thanks
I see you could have two problems here, the first one is that you not specify an action attribute in your form, so first fill it with the url that will handle your form, the second
is that you are checking for None
when an unfilled field is an empty string so you can use:
name = request.form['name']
if name == '':
# do something
Alternatively if you want to fill the field with a default value when it not exists you can use get
dict method as:
name = request.form.get('name', None)
if name is None:
# do something
As other alternative you can use:
if not name:
# do something
cause ''
evaluate to False
in a boolean expression.
Try this:
if request.method == "POST":
ID = request.form['ID']
if not ID=="":
#do stuff`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With