Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottlepy/Flask - How to set a checkbox?

I am trying to set a checkbox if it is checked in a past template. In other way if the user check the checkbox and click on submit button, he should be able to see what options he has checked. My code is like this:

if request.GET.get('submit', '').strip():
    checkbox = request.GET.get('box1')
    return template('my_template.j2', box1 = checkbox)

How can I do it?

like image 347
Andrea Spinelli Avatar asked Mar 11 '23 06:03

Andrea Spinelli


1 Answers

In your template file you can add the following:

<input type="checkbox" name="box1" value="box1" {{'checked="checked"' if box1 else ""}}/>

You can use the python objects passed into the template inside the curly braces, you can find more information in the documentation for inline expressions.

like image 142
mwil.me Avatar answered Mar 24 '23 20:03

mwil.me



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!