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
Written by Morteza Nourelahi Alamdari
Related protips
1 Response
how to set checkbox value?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#