gpt4 book ai didi

python - 如何在 WTForms 中生成动态字段

转载 作者:IT老高 更新时间:2023-10-28 21:02:02 25 4
gpt4 key购买 nike

我正在尝试根据本文档 http://wtforms.simplecodes.com/docs/1.0.2/specific_problems.html#dynamic-form-composition 在 WTForms 中生成具有动态字段的表单

我有这个子表单类,它允许用户从列表中选择要购买的元素:

class Item(Form):
itmid = SelectField('Item ID')
qty = IntegerField('Quantity')

class F(Form):
pass

会有不止一个类别的购物项目,所以我想根据用户将选择的类别生成一个动态选择字段:

fld = FieldList(FormField(Item))
fld.append_entry()

但我收到以下错误:

AttributeError: 'UnboundField' object has no attribute 'append_entry'

是我做错了什么,还是没有办法在 WTForms 中做到这一点?

最佳答案

我今晚遇到了这个问题,最后得到了这个。我希望这对 future 的人有所帮助。

RecipeForm.py

class RecipeForm(Form):
category = SelectField('Category', choices=[], coerce=int)
...

views.py

@mod.route('/recipes/create', methods=['POST'])
def validateRecipe():
categories = [(c.id, c.name) for c in g.user.categories.order_by(Category.name).all()]
form = RecipeForm(request.form)
form.category.choices = categories
...

@mod.route('/recipes/create', methods=['GET'])
def createRecipe():
categories = [(c.id, c.name) for c in g.user.categories.order_by(Category.name).all()]
form = RecipeForm(request.form)
form.category.choices = categories
return render_template('recipes/createRecipe.html', form=form)

我找到了 post也很有帮助

关于python - 如何在 WTForms 中生成动态字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12850605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com