FlaskTest FieldList表单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FlaskTest FieldList表单相关的知识,希望对你有一定的参考价值。

我有表格:

class EntryForm(CsrfDissableForm):

    id = HiddenField()
    name = StringField(...)
    ...

class FooForm(FlaskForm):
    entries = FieldList(FormField(EntryForm))
    new_name = StringField(...)
    ...

在途中:

...
    while len(form.entries) > 0:
        form_entry = form.entries.pop_entry()
        save_changes(form_entry)
    new_inst = form.new_name.data
    save_new(new_inst)
...

我写了一些测试(使用了Flask-Test)。我将'new_name'从发布数据保存到数据库,然后将其编辑为'name'(条目形式):

def test_change_name(self):
    with self.client:
        self.client.post(url, data='new_name': 'Foo')
        foo = db.get_last_foo()
        self.assertEqual(foo.name, 'Foo')
        self.client.post(url, data='entries': ['id': foo.id, name: 'foo1'])
        foo = db.get_by_id(foo.id)
        self.assertEqual(foo.name, 'Foo')

我在这个测试中得到错误:

TypeError: add_file() got an unexpected keyword argument 'id'.

如何正确测试表单字段列表中的请求?路上我可以错吗?

答案

您需要发布表单值,因为它们将由您的应用程序发布。所以,它看起来像data = “entries-0-id”:foo.id,“entries-0-name”:“foo1”

您可能还需要在测试中包含csrf或禁用csrf。

以上是关于FlaskTest FieldList表单的主要内容,如果未能解决你的问题,请参考以下文章

将 StringField 的 FieldList 条目从邮递员发送到烧瓶

wtforms,CSRF,flask,FieldList

如何使用 FormFields 的 WTForms FieldList?

用数据填充 WTForms FormField FieldList 会在字段中生成 HTML

mysql 协议的FieldList命令包及解析

[HTML]表单标签(form表单域input输入表单label标签select下拉表单textarea文本域)