Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get if checkbox is checked on flask

I'm using Bootstrap with Flask Python.

 request.form.get("name")
 #name is the name of the form element(checkbox)

 <label class="btn btn-danger pzt active">
     <input type="checkbox" name="name" value="1" data-id="0"> Check
 </label>

When checkbox is checked, parent label has class "active", I want to get if checked box is checked. Is there any way or methods?

like image 547
Alpan Karaca Avatar asked Jan 06 '14 01:01

Alpan Karaca


People also ask

How do you know if a checkbox is checked?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

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

Is there a way to do this? If you give all the checkbox widgets the same name attribute, and a value attribute with the corresponding name, the form should send a list of names. You can retrieve this using the getlist() method of request.


1 Answers

you can try the following:

HTML:

<div class="checkbox">
<label>
    <input type="checkbox" name="check" value="edit"> New entry
</label>
</div>

In flask:

 value = request.form.getlist('check') 

This will give you the value of the the checkbox. Here value will be a list.

value = [u'edit']

You can also get value of multiple checkboxes with same name attribute.

like image 141
anurag619 Avatar answered Sep 18 '22 20:09

anurag619