pkthoa
Last Updated: February 25, 2016
·
18.65K
· Mortezaipo

Read checkbox in Flask

If you have a list of checkboxes in your forms , it's a little different to get data access.
For example for the simple input tag with 'username' name , we can try it:

if request.method == "POST":
    return request.form.get('username')

Or we can get data via flask_wtf , like:

form = myform()
if request.method == "POST":
    if form.validate_on_submit():
        return form.username.data

Ok , for checkbox , assume that we have list of users that wanna select some of them :

template:

{% for i in data %}
    <input type="checkbox" id="id_{{i.username}}" name="users" value="{{i.id}}">
    <label for="id_{{i.username}}">{{i.username}}</label>
{% endfor %}

view:

if request.method == "POST":
    selected_users = request.form.getlist("users")

Note: It does not need to set [] in checkbox name , like name="users[]".

Be success

1 Response
Add your response

23216

how to set checkbox value?

over 1 year ago ·