开发者

Python pyramid - How to use checkboxes and radio buttons

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 exa开发者_如何转开发mples in pyramid's documentation are simple text fields. I didn't find any radio button or checkboxes so far.


Have you tried put

defaults={"groups" : True}

in Form ctor, for example (in pyramid_simpleform doc):

form = Form(request, MySchema, defaults={"name" : "foo"})


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜