从GET请求填充字段列表?

时间:2014-03-20 11:46:06

标签: flask flask-wtforms

我有一个接受字符串列表的表单:

class MyForm(Form):
    __orders__ = None
    order_by = FieldList(StringField('order_by'))

这是我的观点:

@app.route("/")
def my_view():
    form = MyForm(request.args)
    print form.order_by.entries

我尝试使用以下网址请求网址:

/?order_by=hello&order_by=world
/?order_by[]=hello&order_by[]=world
/?order_by%5B%5D=hello&order_by%5B%5D=world

form.order_by.entries始终为空([]

我错过了什么?

2 个答案:

答案 0 :(得分:0)

好的,我是这样做的,使用SelectMultipleField,但无需处理选择:

class MultipleTextField(SelectMultipleField):
    """
    No different from a normal select field, except this one can take (and
    validate) multiple choices.  You'll need to specify the HTML `rows`
    attribute to the select field when rendering.
    """
    widget = widgets.Select(multiple=True)
    coerce = str
    choices = []

    def pre_validate(self, form):
        pass

答案 1 :(得分:0)

我没有对此进行测试,但查看源代码似乎可能会有效:

/?order_by-0=hello&order_by-1=world