如何使用Flask / WTForms预先填充复选框

时间:2013-10-24 11:00:16

标签: python flask wtforms

我正在尝试生成一个动态复选框列表,其中某些框根据数据状态进行检查。

这是我的表格:

class FooForm(Form):
    bar = SelectMultipleField(
        'Bar',
        option_widget=CheckboxInput(),
        widget=ListWidget(prefix_label=True))

这是控制器:

@app.route('/fooform', methods = ['GET','POST'])
def foo():
    foos = foo_dao.find() 
    form = FooForm()
    form.bar.choices = [(foo.id, foo.label) for foo in foos]
    # SOMEHOW PRE-POPULATE CHECKBOXES HERE
    if form.is_submitted():
        # DO STUFF
    return render_template('foo.html', 
                           foos=foos,
                           form=form)

这是模板:

  <form action="" method="post" name="foos">
      {{form.bar}}
    <p><input type="submit" value="Add"></p>
  </form>

这会生成一个复选框列表,但它可以工作,但我无法弄清楚如何指定列表中的哪些复选框要预先填充。

4 个答案:

答案 0 :(得分:4)

使用FormFieldFieldList 的组合:为了节省一点额外的锅炉板并在持续时间进行一些手动绑定,您可以得到类似的结果将您的提交分成多个字段。

这可以从 DRYer WTForms 方法中获益。如果你遵守它,你会发现你的表格更顺畅。这是因为您正在使用库中内置的默认行为。尽管该库允许您混合和匹配Widget类,但我的经验是,有一个相当有限的组合子集可以很好地协同工作。如果您坚持使用基本的Field / Validator组合,并使用FormField / FieldList进行组合,则效果会更好。

请参阅以下示例:

代码

from collections import namedtuple
from wtforms import Form, FieldList, BooleanField, HiddenField, FormField
from webob.multidict import MultiDict

GroceryItem = namedtuple('GroceryItem', ['item_id', 'want', 'name'])

class GroceryItemForm(Form):
    item_id = HiddenField()
    want = BooleanField()

class GroceryListForm(Form):
    def __init__(self, *args, **kwargs):
        super(GroceryListForm, self).__init__(*args, **kwargs)

        # just a little trickery to get custom labels
        # on the list's checkboxes
        for item_form in self.items:
            for item in kwargs['data']['items']:
                if item.item_id == item_form.item_id.data:
                    item_form.want.label ='' 
                    item_form.label = item.name

    items = FieldList(FormField(GroceryItemForm))

item1 = GroceryItem(1, True, 'carrots')
item2 = GroceryItem(2, False, 'cornmeal')

data = {'items': [item1, item2]}

form = GroceryListForm(data=MultiDict(data))

print form.items()

原始HTML

<ul id="items">
   <li>
      carrots 
      <table id="items-0">
         <tr>
            <th></th>
            <td><input id="items-0-item_id
               " name="items-0-item_id" type="hidden" value="1"><input checked id="items-0-want" name="it
               ems-0-want" type="checkbox" value="y"></td>
         </tr>
      </table>
   </li>
   <li>
      cornmeal 
      <table id="items
         -1">
      <tr>
         <th></th>
         <td><input id="items-1-item_id" name="items-1-item_id" type="hidden" valu
            e="2"><input id="items-1-want" name="items-1-want" type="checkbox" value="y"></td>
      </tr>
      </t
      able>
   </li>
</ul>

呈现结果

enter image description here

POST后form.data的结果

{'items': [{'item_id': 1, 'want': True}, {'item_id': 2, 'want': False}]}

答案 1 :(得分:3)

我认为jeverling的答案非常接近,并引导我找到经过测试的解决方案。我需要检查项目,但每次提供服务时,除非您可以指定选项,否则清除复选框项目。

重要的部分是继承自object的ChoiceObj(上面是MyObj),因此可以在其上调用setattr。为了使这个工作,setattr(obj,attribute,value)的参数在哪里

  • obj是ChoiceObj实例
  • 属性是表单的名称
  • 在选项列表中设置的值。

color.py:

from flask.ext.wtf import Form
from flask import Flask, render_template, session, redirect, url_for
from wtforms import SelectMultipleField, SubmitField, widgets

SECRET_KEY = 'development'

app = Flask(__name__)
app.config.from_object(__name__)

class ChoiceObj(object):
    def __init__(self, name, choices):
        # this is needed so that BaseForm.process will accept the object for the named form,
        # and eventually it will end up in SelectMultipleField.process_data and get assigned
        # to .data
        setattr(self, name, choices)

class MultiCheckboxField(SelectMultipleField):
    widget = widgets.TableWidget()
    option_widget = widgets.CheckboxInput()

    # uncomment to see how the process call passes through this object
    # def process_data(self, value):
    #     return super(MultiCheckboxField, self).process_data(value)

class ColorLookupForm(Form):
    submit = SubmitField('Save')
    colors = MultiCheckboxField(None)

allColors = ( 'red', 'pink', 'blue', 'green', 'yellow', 'purple' )

@app.route('/', methods=['GET', 'POST'])
def color():
    selectedChoices = ChoiceObj('colors', session.get('selected') )
    form = ColorLookupForm(obj=selectedChoices)
    form.colors.choices =  [(c, c) for c in allColors]

    if form.validate_on_submit():
        session['selected'] = form.colors.data
        return redirect(url_for('.color'))
    else:
        print form.errors
    return render_template('color.html',
                           form=form,
                           selected=session.get('selected'))

if __name__ == '__main__':
    app.run()

和templates / color.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <form method="post">
        <table>
            <tr>
                <td>
                    {{ form.hidden_tag() }}
                    {{ form.colors }}
                </td>
                <td width="20"></td>
                <td>
                    <b>Selected</b><br>
                    {% for s in selected %}
                        {{ s }}<br>
                    {% endfor %}
                </td>
            </tr>

        </table>
        <input type="submit">
    </form>
</body>
</html>

答案 2 :(得分:2)

我使用内部子类来解决这个问题:

http://wtforms.simplecodes.com/docs/1.0.1/specific_problems.html#dynamic-form-composition

您必须在初始化表单之前设置选项,您可以使用内部子类。 然后,您可以将一个对象传递给Form,wtforms将使用它来预填充您的字段:

def foo():
    class F(FooForm):
        pass
    choices = [(foo.id, foo.label) for foo in foos]
    F.bar.choices = choices

    class MyObj(object):
        pass
    obj = MyObj()
    for choice in choices:
        setattr(obj, choice, True)

    form = F(request.POST or None, obj=obj)

请注意,这不是未经测试的,但我认为它应该有效 祝你好运!

答案 3 :(得分:1)

WTForms 的 SelectMultipleField 通过将每个项目的值(在您的示例中为 foo.id)与字段的 .data 列表进行比较来确定是否选中一个框。 (检查 WTForms 源代码以验证这一点。)

我就这样解决了这个问题。我的目标是预先选中每个框,所以我包含了该选项,注释掉了:

@app.route('/fooform', methods = ['GET','POST'])
def foo():
    foos = foo_dao.find() 
    form = FooForm()
    form.bar.choices = [(foo.id, foo.label) for foo in foos]
    if form.is_submitted():
        for x in form.bar.data:
            #ACT ON EACH USER-SELECTED ITEM HERE
    else: #precheck the boxes here
        form.bar.data = [1,2,5,8] #id's of items to be prechecked on form
        #form.bar.data = [x[0] for x in form.bar.choices] #this prechecks every box
    return render_template('foo.html', 
                           foos=foos,
                           form=form)