How Do I Make Multiple Checkbox With A Unique Id Using WTForms So That I Can Store It In Shelves?
How can I make multiple checkbox using WTForms and render it to my html page? This is what i've come up with using WTForms but i was told that by using Boolean(True,False) it will
Solution 1:
Please let me know if it helps. In the end, a dictionary (data) is returned with the names of the checked checkboxes.
Flask code:
@app.route('/index/', methods=['POST', 'GET'])
def index():
weekdays = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
timeslots = ['8AM', '9AM', '10AM', '11AM', '12PM', '13PM', '14PM','15PM','16PM','17PM','18PM']
if request.method == "POST":
data = dict((key, request.form.getlist(key) if len(
request.form.getlist(key)) > 1 else request.form.getlist(key)[0])
for key in request.form.keys())
print (data) # do whatever you want with this data dict. Store it, flash messages, etc.
return render_template('index.html',
weekdays = weekdays,
timeslots = timeslots,
)
The template
<form method = "post" action = "">
<table class="table table-bordered">
<thead>
{% for weekday in weekdays %}
<tr>
<th> {{ weekday }} </th>
{% for timeslot in timeslots %}
<td><input id="{{ weekday+timeslot }}" type="checkbox" name="{{ weekday+timeslot }}">{{ timeslot }}</td>
{% endfor %}
</tr>
{% endfor %}
</thead>
</table>
<div class="form-group col-md-1">
<input type="submit" value="Submit" class="btn btn-info"/>
</div>
</form>
Post a Comment for "How Do I Make Multiple Checkbox With A Unique Id Using WTForms So That I Can Store It In Shelves?"