问题
I've been trying to make a form with checkboxes and radio button using Pyramid framework but I can't figure out how to do it properly.
I'm using the pyramid_simpleform
. So far I've been able to put my checkboxes on the form using a for loop but I can't make any checkbox checked even if I specify checked=True
.
% for item in groups:
${form.checkbox(name="groups",label=item, value=item, checked=True)}
% endfor
I know there's a better way of doing this. Is there any examples I could look at. All the examples in pyramid's documentation are simple text fields. I didn't find any radio button or checkboxes so far.
回答1:
Have you tried put
defaults={"groups" : True}
in Form ctor, for example (in pyramid_simpleform doc):
form = Form(request, MySchema, defaults={"name" : "foo"})
回答2:
I use FormRenderers to output forms and also had problems using Checkboxes. So I wrote the following class that replaces the FormRenderer from simple_form in all my views:
# -*- coding: utf-8 -*-
from pyramid_simpleform.renderers import FormRenderer as OldFormRenderer
from webhelpers.html import tags
class FormRenderer(OldFormRenderer):
def checkbox(self, name, value="1", checked=False, label=None, id=None,
**attrs):
"""
Outputs checkbox input.
"""
id = id or name
return tags.checkbox(name, value, checked, label, id, **attrs)
来源:https://stackoverflow.com/questions/5400397/python-pyramid-how-to-use-checkboxes-and-radio-buttons