I am trying to retrieve the labels (or values) of the checkboxes that the user has selected and return them to a different page. This logic is working for text entries, select options, and radios, all of which are included in that same python post method (so I know the method is working) but I cannot get it to work with checkboxes.
***If I set the boxes to checked as default by inserting “checked” at the end of the input line in form.tpl, then the first movie is listed, but if another movie is referenced in results, then it throws an error saying the second movie is undefined.
***If I do not set the boxes as checked by default, the movies will not appear even when the user checks the box in the form and submits.
form.tpl:
<body> <form action="/form/results" class="needs-validated" method="post"> <div class="form-check"> <input type="checkbox" class="form-check-input" id="movie1" name="movie1" value="Kill Bill Vol. II"> <label class="form-check-label" for="movie1">Kill Bill Vol. II</label> <input type="checkbox" class="form-check-input" id="movie2" name="movie2" value="Fight Club"> <label class="form-check-label" for="movie2">Fight Club</label> </div> </form> </body>
test.py:
@post('/form/results') def show_results(): movie1 = request.forms.get('movie1') movie2 = request.forms.get('movie2') return template('results', movie1=movie1, movie2=movie2)
results.tpl:
<body> <p>Title(s) of movie(s) chosen: {{movie1}} {{movie2}}</p> </body>
Advertisement
Answer
The checkbox fields are coming through for me, but as either "on"
or the value you provided (if checked) or None
(if unchecked), which seems sufficient to infer the state. Here’s a minimal example of passing checked state through each page:
server.py
from bottle import post, request, route, run, template @post("/form/results") def show_results(): movie1 = request.forms.get("movie1") movie2 = request.forms.get("movie2") print(repr(movie1), repr(movie2)) return template("form.tpl", movie1=movie1, movie2=movie2) @route("/form/results") def items_(): return template("form.tpl", movie1=None, movie2=None) if __name__ == "__main__": run(host="localhost", port=8080, debug=True, reloader=True)
form.tpl
<!DOCTYPE html> <html> <body> <form action="/form/results" method="post"> <input type="checkbox" name="movie1" value="Kill Bill Vol. II" {{"checked" if movie1 else ""}} > <label class="form-check-label" for="movie1">Kill Bill Vol. II</label> <input type="checkbox" name="movie2" value="Fight Club" {{"checked" if movie2 else ""}} > <label class="form-check-label" for="movie2">Fight Club</label> <input type="submit"> </form> </body> </html>
Output on the terminal after a couple of requests checking one or the other of the boxes:
127.0.0.1 - - [18/Mar/2022 12:17:04] "GET /form/results HTTP/1.1" 200 499 127.0.0.1 - - [18/Mar/2022 12:17:06] "GET /sw-debug.js HTTP/1.1" 404 742 None 'Fight Club' 127.0.0.1 - - [18/Mar/2022 12:17:08] "POST /form/results HTTP/1.1" 200 506 127.0.0.1 - - [18/Mar/2022 12:17:10] "GET /sw-debug.js HTTP/1.1" 404 742 'Kill Bill Vol. II' None 127.0.0.1 - - [18/Mar/2022 12:17:17] "POST /form/results HTTP/1.1" 200 506 127.0.0.1 - - [18/Mar/2022 12:17:19] "GET /sw-debug.js HTTP/1.1" 404 742