Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of a checkbox in Flask

I want to get the value of a checkbox in Flask. I've read a similar post and tried to use the output of request.form.getlist('match') and since it's a list I use [0], but it seems I'm doing something wrong. Is this the correct way to get the output or is there a better way?

<input type="checkbox" name="match" value="matchwithpairs" checked> Auto Match 
if request.form.getlist('match')[0] == 'matchwithpairs':     # do something 
like image 565
Jaquacky Avatar asked Aug 06 '15 15:08

Jaquacky


People also ask

How do I get the value of a checkbox in Python?

To print the value of the selected checkbox, we can use the get() method. It returns the input value of a particular widget.

How can I tell if a checkbox is checked in flask?

If you want to know if its checked on the server side, just check if that form field exists, if request. form. get("name") gives you NULL or exception, then the checkbox should be unchecked.

How do you make a checkbox in flask?

First, set the current value of the name attribute to the value attribute. This will determine values Flask pulls during the form post. Secondly, set the name attribute to have a common value. After the HTML is configured as such, you can use the getlist method from Flask's request module.

How do I make a checkbox in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!

What is the correct order of checkboxes in flask?

I am using flask and I have had an HTML page with a bunch of checkboxes. They are ordered as option1, option2, and option3 and then under the options, there are a number of boxes to check.

How to get data from a list of checkboxes in forms?

If you have a list of checkboxes in your forms , it's a little different to get data access. Or we can get data via flask_wtf , like: Ok , for checkbox , assume that we have list of users that wanna select some of them : Note: It does not need to set [] in checkbox name , like name="users []".

How do I submit a form with both boxes checked?

Submitting the form with both boxes checked prints ['world', 'davidism'] in the terminal. Note that the html form's method is post so that the data will be in request.form. While there are some cases where knowing the actual value or list of values of an field is useful, it looks like all you care about is whether the box was checked.

Why can't I check every item in a form?

the problem is that you have a form-element for each product, so if you want to check every item you need to wrapp all these items with the form-element. Another option is to use javascript as I mentioned in your other question. then you could remove all form elements and would use a javascript function instead.


2 Answers

You don't need to use getlist, just get if there's only one input with the given name, although it shouldn't matter. What you've shown does work. Here's a simple runnable example:

from flask import Flask, request  app = Flask(__name__)  @app.route('/', methods=['GET', 'POST']) def index():     if request.method == 'POST':         print(request.form.getlist('hello'))      return '''<form method="post"> <input type="checkbox" name="hello" value="world" checked> <input type="checkbox" name="hello" value="davidism" checked> <input type="submit"> </form>'''  app.run() 

Submitting the form with both boxes checked prints ['world', 'davidism'] in the terminal. Note that the html form's method is post so that the data will be in request.form.


While there are some cases where knowing the actual value or list of values of an field is useful, it looks like all you care about is whether the box was checked. In this case, it's more common to give the checkbox a unique name and just check if it has any value at all.

<input type="checkbox" name="match-with-pairs"/> <input type="checkbox" name="match-with-bears"/> 
if request.form.get('match-with-pairs'):     # match with pairs  if request.form.get('match-with-bears'):     # match with bears (terrifying) 
like image 174
davidism Avatar answered Sep 21 '22 15:09

davidism


I found 4 ways to do that: Just to summarize:

# first way op1 = request.form.getlist('opcao1') # [u'Item 1'] [] op2 = request.form.getlist('opcao2') # [u'Item 2'] [] op3 = request.form.getlist('opcao3') # [u'Item 3'] []  # second op1_checked = request.form.get("opcao1") != None op2_checked = request.form.get("opcao2") != None op3_checked = request.form.get("opcao3") != None  # third if request.form.get("opcao3"):     op1_checked = True  # fourth op1_checked, op1_checked, op1_checked = False, False, False if request.form.get("opcao1"):     op1_checked = True if request.form.get("opcao2"):     op2_checked = True if request.form.get("opcao3"):     op3_checked = True  # last way that I found .. op1_checked = "opcao1" in request.form op2_checked = "opcao2" in request.form op3_checked = "opcao3" in request.form 
like image 26
Silas Santiago Avatar answered Sep 22 '22 15:09

Silas Santiago