繁体   English   中英

WTForms SelectField 返回 None

[英]WTForms SelectField returning None

我正在使用 WTForms 和 Flask,我正在尝试创建一个表单,我可以在其中输入有关配方的信息,但是 product_name SelectField 每次都返回 None 。

表格:

class CreateRecipeForm(Form):
    product_name = SelectField(choices=get_craftables_options())
    product_quantity = IntegerField(default=1)
    job_field = SelectField(choices=['ALC', 'GSM', 'WVR'])
    line_item_list = FieldList(FormField(RecipeLineForm), min_entries=6)
    save_button = SubmitField()

风景:

@bp.route('/edit/new', methods=('GET', 'POST'))
def create_recipe():
    form = CreateRecipeForm()
    if request.method == 'POST':
        selected_product = Item.query.get(form.product_name.data)
        (do stuff here)

模板

{% block content %}
    <form method="post">
        {{ render_field(form.product_name) }}
        {{ render_field(form.product_quantity) }}
        {{ render_field_no_label(form.line_item_list) }}
        {{ render_field_no_label(form.save_button) }}
    </form>
{% endblock %}

我相信您的问题在于声明 product_name。 确保 get_craftables_options() 应该是一个函数,并返回与选择参数兼容的项目列表。

product_name = SelectField(choices=get_craftables_options())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM