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 2 (modified by nic at bellamy dot co dot nz, 14 years ago) (diff)

Reformatted; added dynamic example

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:
    def index(self):
        items =
        # 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" value="${}" py:attrs="checked=std.checker( in items_selected)"/>${i.label}</label>