Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how I check in flask that field from form was filled by user

Tags:

python

flask

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

like image 244
user1743947 Avatar asked Jun 06 '13 14:06

user1743947


2 Answers

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.

like image 87
angvillar Avatar answered Nov 14 '22 21:11

angvillar


Try this:

if request.method == "POST":
     ID = request.form['ID']
           if not ID=="":
               #do stuff`
like image 24
Kevin Taylor Avatar answered Nov 14 '22 23:11

Kevin Taylor