wiki:CheckedCheckBox
Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 3 (modified by nic at bellamy dot co dot nz, 14 years ago) (diff)

Added name= to input (duh)

Automatically checking a checkbox or radio button

The following:

<input type="checkbox" py:attrs="checked=std.checker(expression)" />

will create a checked checkbox if the expression is true.

Dynamic checkboxes

To create a dynamic list of checkboxes (or radio buttons), with the appropriate ones pre-checked:

from model import YourDataType

class YourController:
    @turbogears.expose(...)
    def index(self):
        items = YourDataType.select()
        # Select some examples items; in reality, these would be pulled from another SQLObject or other source
        items_selected = [4, 5, 9]
        return dict(items = items, items_selected = items_selected)

Then in the template:

<!-- Using py:strip below means that the div will not be in the resulting HTML; we're just using it as a wrapper -->
<div py:for="i in items" py:strip="True">
  <label><input type="checkbox" name="selecteditems" value="${i.id}" py:attrs="checked=std.checker(i.id in items_selected)"/>${i.label}</label>
</div>