October 1st, 2010
Web.py checkboxes
Checkboxes are a bit tricky with web.py, here’s how to get them working:
import web
from web import form
example_form = form.Form(
form.Checkbox("lovelycheckbox", description="lovelycheckbox", class_="standard", value="something.. Anything!"),
form.Button("Update checkbox", type="submit", description="Send"),
)
class grabresults:
def GET(self):
f = example_form()
return f.render()
def POST(self):
f = example_form()
theresult = f['lovelycheckbox'].checked
OK, so that’s a dumb (and untested) example, but it gives you an idea. The ’theresult’ variable is going to be true or false depending on if the ’thelovelycheckbox’ was ticked or not.
Things to note:
- There’s a web.py bug which means that you have to give the checkbox a value attribute for them to work.
- To get the result, you must use
.checked
instead of the normal.value
attribute.
Cheers, Dave.