我在Pylons 1中使用过formalchemy后,用pyramid_simpleform移动了我的第一步。
我开始使用的表单很简单:我有一些选项,用户必须检查一个。
这里是表单类:
class OptionsSchema(Schema):
...
id_opt = validators.Int(not_empty=True)
这里是它的实例:
model = DBSession.query(models.Model).first()
form = Form(request, schema=OptionsSchema, obj=model)
renderer = FormRenderer(form)
让我们说model.id_opt == 3
,
在此处,我为模板中的option
中的每个model
制作一个单选按钮:
%for opt in model.options:
${form.radio('id_opt', value=opt.id)}
%endfor
我希望看到的checked="checked"
是id_opt == 3
,如下所示:
<input id="id_opt_1" name="id_opt" type="radio" value="1" />
<input id="id_opt_2" name="id_opt" type="radio" value="2" />
<input id="id_opt_3" checked="checked" name="id_opt" type="radio" value="3" />
但我没有。
我必须自己设置选中选项吗?