【问题标题】:WTForms SelectField returning NoneWTForms SelectField 返回无
【发布时间】:2021-12-20 04:41:26
【问题描述】:

我正在使用 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 %}

【问题讨论】:

  • 你的get_craftables_options()函数是什么样的?

标签: python flask flask-wtforms wtforms


【解决方案1】:

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

product_name = SelectField(choices=get_craftables_options())

【讨论】:

    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 2023-03-27
    • 2016-09-10
    • 2020-02-27
    • 2021-10-23
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    相关资源
    最近更新 更多