WTForm:带有SelectField的FieldList,如何渲染? [英] WTForm: FieldList with SelectField, how do I render?

查看:235
本文介绍了WTForm:带有SelectField的FieldList,如何渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此订单,可让我的用户创建订单.一个订单由多个(producetype, quantity)元组组成. Producetype应该以<select>形式呈现,而数量只能作为输入.生产类型的选择应动态添加,因为这可能会发生变化.目前,我已经用裸露的

I have this order form which allows my users to create an order. An order consists of multiple tuples of (producetype, quantity). Producetype should be rendered in a <select> form while quantity can just be an input. The choices of producetype should be dynamically added because that could change. Currently, I've written this in bare html

我想为此使用WTForm,因为WTForm确实简化了我的代码.但是,我无法这样做:

I would like to use WTForm for this because WTForm really simplifies my code. However, I am unable to do so:

代码:

class OrderEntryForm(Form):
  quantity = IntegerField('Quantity',
                          [validators.Required(), validators.NumberRange(min=1)])
  # we will be dynamically adding choices
  producetype = SelectField('Produce',
                            [validators.Required()],
                            choices=[])

class OrderForm(Form):
  name = TextField('Crop', [validators.Length(min=3, max=60)])
  due_date = DateField('Due Date', [validators.required()])
  order_entries = FieldList(FormField(OrderEntryForm))

我有以下问题:

  1. 如何动态地将选择添加到OrderForm的order_entries字段中?
  2. 如果我有订单order = { name:hello, due_date:2014-06-18, order_entries:[ {producetype_id: 1, quantity: 2}, {producetype_id: 3, quantity: 4}] },如何用正确的OrderEntryForm值填充我的OrderForm?
  1. How can I dynamically add choices to the order_entries field of the OrderForm?
  2. If I have an order, order = { name:hello, due_date:2014-06-18, order_entries:[ {producetype_id: 1, quantity: 2}, {producetype_id: 3, quantity: 4}] }, how can populate my OrderForm with the right OrderEntryForm values?

我的代码在这里可用: https://gist.github.com/vicngtor/f8c8f0519dbd6b3b6110

My code is available here: https://gist.github.com/vicngtor/f8c8f0519dbd6b3b6110

推荐答案

如何动态地将选择添加到OrderForm的order_entries字段中

How can I dynamically add choices to the order_entries field of the OrderForm

这取决于您的意思

如果您要在渲染后将行项目添加到表单中.您必须使用DOM操作将它们添加到客户端. WTForms 具有用于处理表单字段索引的命名约定.它只是name="<form-field)name>-<index>".如果您使用遵循此约定的javascript添加名称,则 WTForms 将知道如何在后端处理它.

If you mean you want to add lines items to the form after render. You have to use DOM manipulation to add these on the client side. WTForms has a naming convention for addressing indices on form fields. Its just name="<form-field)name>-<index>". If you add a name using javascript that follows this convention WTForms will know how to handle it on the backend.

如果您想拥有一个动态选择列表,则可以在实例化后在表单中的FieldList上进行迭代.您可以为choices分配2元组的任何迭代.在下面的示例中,我直接分配了它们,但是可以很容易地从存储中检索它们.

If you mean you want to have a dynamic choice list then you can just iterate over the FieldList in the form after its instantiated. You can assign choices any iterable of 2-tuples. In the example below I am assigning them directly but they could just as easily have been retrieved from storage.

order_form = OrderForm()
for sub_form in order_form.order_entries:
    sub_form.producetype.choices = [('2', 'apples'), ('2', 'oranges')]

如何用正确的OrderEntryForm值填充我的OrderForm?

how can populate my OrderForm with the right OrderEntryForm values?

您可以使用obj关键字参数将对象直接绑定到表单. WTForms 足够聪明,可以根据对象的属性动态构建表单.

You can just bind objects directly to the form using the obj keyword argument. WTForms is smart enough to dynamically build the form from the object's attributes.

from wtforms import Form, IntegerField, SelectField, TextField, FieldList, FormField
from wtforms import validators
from collections import namedtuple

OrderEntry = namedtuple('OrderEntry', ['quantity', 'producetype'])
Order = namedtuple('Order', ['name', 'order_entries'])

class OrderEntryForm(Form):
  quantity = IntegerField('Quantity',
                          [validators.Required(), validators.NumberRange(min=1)])
  # we will be dynamically adding choices
  producetype = SelectField('Produce',
                            [validators.Required()],
                            choices=[
                                (1, 'carrots'),
                                (2, 'turnips'),
                            ])

class OrderForm(Form):
  name = TextField('Crop', [validators.Length(min=3, max=60)])
  order_entries = FieldList(FormField(OrderEntryForm))

# Test Print of just the OrderEntryForm
o_form = OrderEntryForm()
print o_form.producetype()

# Create a test order
order_entry_1 = OrderEntry(4, 1)
order_entry_2 = OrderEntry(2, 2)

order = Order('My First Order', [order_entry_1, order_entry_2])

order_form = OrderForm(obj=order)

print order_form.name
print order_form.order_entries

上面的示例创建一个示例Order,并将其提供给obj关键字.在渲染时,将生成以下(未样式化):

The above example creates a sample Order and supplies it to the obj keyword. On render this will generate the following(unstyled):

这篇关于WTForm:带有SelectField的FieldList,如何渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆