문제

의문:

값으로 toscawidgets에서 checkboxtable을 시사하는 방법

배경:

나는 어디에서나 보았고 toscawidgets로 특정 양식 필드를 초기화하는 방법을 알 수없는 것 같습니다. 대부분의 양식 필드는 템플릿에서 양식을 렌더링하고 FieldValue = x에서 전달할 때 단일 텍스트 필드가있는 양식을 작성하는 것처럼 초기화에 잘 맞는 것 같습니다. 텍스트 필드는 x로 채워집니다. 내 문제는 모든 다중 선택 필드, 특히 Checkboxtable입니다. 내가 무엇을 통과하든 다중 선택을 초기화하지 않습니다. 다음은 제가 말하는 내용의 예입니다. 그룹에 대한 Checkboxtable이있는 사용자 편집 페이지이므로 데이터베이션에서 가져온 여러 그룹의 목록에서 여러 그룹을 선택할 수 있습니다.

내가 가진 것 :

내 위젯은 다음과 같습니다.

from tw import forms
class UserForm(forms.TableForm):

    show_errors = True
    submit_text = "Create User"

    clientOptions = [(-1, "Select a Client")]
    groupOptions = [(-1, "Select a Group")]

    fields = [forms.TextField('name', label_text='User Name', validator=String(not_empty=True), size=40),
              forms.Spacer(),
              forms.SingleSelectField('clientID', label_text='Client Name', validator=Int(min=0), options=clientOptions),
              forms.Spacer(),
              forms.CheckBoxTable('groups', lable_text='Groups', validator=Set(), options=groupOptions, num_cols=3),
              forms.Spacer(),
              forms.PasswordField('password', label_text="Password", validator=String(not_empty=True, min=6), size=40),
              forms.PasswordField('passwordAgain', label_text="Repeat Password", validator=String(not_empty=True, min=6), size=40),
              forms.HiddenField('id')]

editUserForm = UserForm("createUserForm", action='alterUser', submit_text="Edit User")

내 컨트롤러에는 다음과 같습니다.

result = model.DBSession.query(model.User).filter_by(id=kw['id']).first()
tmpl_context.form = editUserForm
clientOptions=model.DBSession.query(model.Client.id, model.Client.name)
groupOptions=model.DBSession.query(model.Group.id, model.Group.name)
formChildArgs = dict(clientID=dict(options=clientOptions), groups=dict(options=groupOptions))

userAttributes=dict(id=result.id, name=result.name, groups=[g.id for g in result.groups], clientID=result.clientID, password=result.password, passwordAgain=result.password)

return dict(verb="Edit", modelName = "User", modelAttributes=userAttributes, formChildArgs=formChildArgs, page='editUser')

그리고 내 템플릿 (mako)에는 다음과 같습니다.

${tmpl_context.form(modelAttributes, child_args=formChildArgs) | n}

내가 시도한 것 :

내 userAttRipps 사전에서 나는 시도했다.

groups=[g.id for g in result.groups]
groups=[g.name for g in result.groups]
groups=[(g.id, g.name) for g in result.groups]
groups=[[g.id, g.name) for g in result.groups]
groups=result.groups

내가 얻는 것 :

이 모든 코드의 결과는 Checkboxtable을 제외한 사용자 데이터로 미리 채워진 데이터가있는 사용자 편집 양식입니다. Checkboxtable에는 데이터베이스의 모든 그룹이 표시되고 비어 있습니다. 표시하는 데 필요한 내용이 있지만 사용자가 확인한 그룹이 있습니다. 모델 속성의 코드 가이 작업을 수행 할 것이라고 생각했습니다. 왜냐하면 그것이 다른 모든 분야에서하는 일이기 때문입니다. 그러나 Checkboxtable 인스턴스화에 대해 놓친 근본적인 일이 있어야합니다.

명세서:

Toscawidgets 0.9.7 양식과 템플릿을 위해 Mako와 함께 Turbogears 2를 사용하고 있습니다.

도움이 되었습니까?

해결책

값 파라를 통해 설정하십시오.

import tw.forms
f = tw.forms.TableForm(fields=[tw.forms.CheckBoxTable("name",options=(("foo"),("bar")))]) 
f(value={"name":{"foo":True,"bar":False}})
>>> u'<form xmlns="http://www.w3.org/1999/xhtml" action="" method="post" class="tableform">\n    <table border="0" cellspacing="0" cellpadding="2">\n<tr id="name.container" class="even" title="">\n            <td class="labelcol">\n                <label id="name.label" for="name" class="fieldlabel">Name</label>\n            </td>\n            <td class="fieldcol">\n                <table id="name" class="checkboxtable">\n    <tbody>\n    <tr>\n        <td>\n
    <input id="name_0" value="foo" name="name" type="checkbox" checked="checked" />\n            <label for="name_0">foo</label>\n        </td>\n    </tr><tr>\n        <td>\n            <input id="name_1" value="bar" name="name" type="checkbox" />\n            <label for="name_1">bar</label>\n        </td>\n    </tr>\n
</tbody>\n</table>\n            </td>\n        </tr><tr id="submit.container" class="odd" title="">\n            <td class="labelcol">\n            </td>\n
       <td class="fieldcol">\n                <input type="submit" class="submitbutton" value="Submit" />\n            </td>\n        </tr>\n    </table>\n</form>'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top