简体   繁体   English

WTForms SelectField 返回 None

[英]WTForms SelectField returning None

I'm using WTForms and Flask, I am trying to create a form where I can enter information about a recipe, but the product_name SelectField is returning None every time.我正在使用 WTForms 和 Flask,我正在尝试创建一个表单,我可以在其中输入有关配方的信息,但是 product_name SelectField 每次都返回 None 。

The form:表格:

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()

The view:风景:

@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)

The template模板

{% 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 %}

I believe your issue lies in declaring the product_name.我相信您的问题在于声明 product_name。 Make sure the get_craftables_options() is supposed to be a function and is returning a list of items compatible with the choices argument.确保 get_craftables_options() 应该是一个函数,并返回与选择参数兼容的项目列表。

product_name = SelectField(choices=get_craftables_options())

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

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