ToscaWidgets2从GrowingGridLayout捕获数据

时间:2016-03-29 15:48:19

标签: python forms turbogears2

目前正在处理包含TurboGears2ToscaWidgets2的项目。我有一个表单设置,其中包含一些静态字段,名称,日期和联系人信息。在此表单中,我添加了一个子表单,用户可以在GrowingGridLayout中动态添加多个条目。表单,布局和提交信息都很好,但我很难确定如何在GrowingGridLayout传递信息后保存信息。猜猜要点是,我怎么知道表格中包含了多少条目?

包含表格的代码:

class OnrampForm(twf.Form):
    title = "Onramp Submission Form"

    class child(twd.CustomisedTableForm):
        onramp_name = twf.TextField(validator=twc.Required)

        class Destinations (twd.GrowingGridLayout):
            environment = twf.SingleSelectField(label='Environment', validator=twc.Validator(required=True), options=[<OPTIONS>])
            location = twf.SingleSelectField(validator=twc.Required, label='Location', options=[<OPTIONS>])
            jms_type = twf.SingleSelectField(label='JMS Type', validator=twc.Validator(required=True), options=[<OPTIONS>])
            subscription_type = twf.SingleSelectField(label='Subscription Type', validator=twc.Validator(required=True), options=[<OPTIONS>])

        onramp_status = twf.SingleSelectField(prompt_text='Status', options=['Initial Release', 'Update'], validator=twc.Required)
        current_date = datetime.date.today()
        need_by_date = twd.CalendarDatePicker(validators=[twc.Required, twc.DateTimeValidator])
        need_by_date.default = current_date + datetime.timedelta(days=30)
        organization = twf.TextField(validator=twc.Required)
        poc_name = twf.TextField(validator=twc.Required)
        poc_email = twf.EmailField(validator=twc.EmailValidator)
        poc_phone = twf.TextField(validator=twc.Required)
        poc_address = twf.TextField()
        poc_city = twf.TextField()
        poc_state = twf.TextField()
        onramp_form = twf.FileField()
        submit = twf.SubmitButton(value="Submit")

    action = "/print_args"
    submit = ""

1 个答案:

答案 0 :(得分:0)

如果控制器@validate针对表单,则应将数据存入Destination参数,该参数应为字典列表。

另外我只是注意到你有两个嵌套形式,这可能会让TW2感到困惑。您想要做的可能是OnrampForm继承CustomisedForm,然后让child继承TableLayout。见http://turbogears.readthedocs.org/en/latest/cookbook/TwForms.html#displaying-forms

PS:请注意,need_by_date.default = current_date + datetime.timedelta(days=30)将始终从服务器启动后30天返回,因为您实际存储的是导入模块时计算的current_date = datetime.date.today()类变量,而不是更多。

您应该使用default = Deferred(lambda: datetime.date.today() + datetime.timedelta(days=30))来实现