Python pyramid - How to use checkboxes and radio buttons

痞子三分冷 提交于 2019-12-21 23:24:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!