Turbogears Toscawidget 不工作

Turbogears Toscawidget not working

我在这个 guide and this guide 中进行了搜索,并按照示例进行了操作。 这是我在 controllers/root.py:

中的控制器
@expose('rubrica.templates.submitForm')
def add(self, *args, **kw):  
    return dict(page='submitForm', form=SubmitForm)

@expose()
def save_record(self, **kw):
    print('save_record')
    new_contact = Contact(name = kw['name'], phone = kw['phone'])
    model.DBSession.add(new_contact)
    flash(message = "Added new contact")
    redirect('/index')

这是SubmitForm.py:

import tw2.core as twc
import tw2.forms as twf
class SubmitForm(twf.Form):
    class child(twf.TableLayout):
        name = twf.TextField(size = 20)
        phone = twf.TextField(size = 20)
        action = '/save_record'
        submit = twf.SubmitButton(value = 'Submit')

这是模板:

<head py:block="head" py:strip="True">
    <title py:block="master_title">Aggiungi Contatto</title>
</head>
<body py:block="body" py:strip="True">
    <div>
        ${form.display(value = dict(title = 'Inserisci i dati'))}
    </div>
</body>
</html>

问题是 save_record 没有被调用(action = '/save_record' 应该调用它,但没有任何反应) 我不明白为什么.. 猜想我做错了什么,或者缺少了什么但我无法理解:)

感谢帮助!

actionsubmitForm 的属性,但您在 Layout 中指定了它们。将 actionsubmit 属性移出父作用域,它应该会按预期工作:

import tw2.core as twc
import tw2.forms as twf

class SubmitForm(twf.Form):
    class child(twf.TableLayout):
        name = twf.TextField(size = 20)
        phone = twf.TextField(size = 20)

    action = '/save_record'
    submit = twf.SubmitButton(value = 'Submit')