python - 在 Flask 应用程序中使用 wtforms 选择字段获取 "Not a valid choice",我做错了什么?

标签 python flask wtforms flask-wtforms

全部。我正在开发 Flask 应用程序。我正在为一家带有在线订购系统的餐厅开发一个网站。我是为一个 friend 做这件事,并将其视为一次学习经历。我要问的问题可能是一个小问题。我不确定这个问题更多的是 wtforms 还是flask-wtf。希望有人能够提供一些见解。

我正在创建一个购物车,其中有一些通过 wtf.quick_form 呈现的按钮。这是:

<div id='cart'>
    <div>
        <p>
        <center><b>Shopping cart</b></center>
        </p>
        <div>
            {% for item in requested_item_record %}
            <div id='cartI'>{{list_of_items[item]['name']}}</div>
            <div id='cartB'> {{ wtf.quick_form(remove_item_dict[item]) }}</div>
            {% endfor %}
        </div>
        <p id='cartT'>Subtotal: {{ total_cost/100 }} </p>

        <p id='cartT'>Tax: {{ tax/100 }}</p>

        <p id='cartT'>Total: {{ total/100 }}</p>

        <div id='cartB'>{{ wtf.quick_form(clear_cart) }}</div>
        <div id='cartB'>{{ wtf.quick_form(order_type) }}</div>
    </div>
</div>

以下是 wtforms 定义的按钮:

class AddItem(Form):
        submit = SubmitField("Add item to cart")

class ClearCart(Form):
        submit = SubmitField("Clear cart")

class RemoveItem(Form):
        submit = SubmitField("Remove item")

class OrderType(Form):
        select = SelectField("Pickup or delivery?", choices=[('pickup','Pickup'),('delivery','Delivery')])
        submit = SubmitField("Proceed to checkout")

问题是我在 OrderTypeSelectField 下方看到“不是有效的选择”按钮。我认为这会让用户感到困惑。我不知道如何删除它。这是屏幕截图:

enter image description here

只有当我使用 RemoveItem 按钮或 ClearCart 按钮之一提交数据时,我才会看到该行为。

这是我的 View 函数的相关部分:

# generate add buttons for all items of the menu
button_dict = OrderedDict([])
for i in range(number_of_submit_buttons):
    button_dict[i] = AddItem(prefix='add item ' + str(i))
clear_cart = ClearCart(prefix="clear cart")

# check for submit data from users, remove an item or clear cart.
for iterable, add_button in enumerate(button_dict):
    if button_dict[add_button].validate_on_submit and button_dict[add_bu$
        session['requested_item_record'].append(iterable)
        session['total_cost'] += list_of_items[iterable]['price']
if clear_cart.validate_on_submit and clear_cart.submit.data:
    session['requested_item_record'] = []
    session['total_cost'] = 0

# select field, for delivery or pickup
order_type = OrderType(prefix='order_type')
if order_type.validate_on_submit() and order_type.submit.data:
        if order_type.select.data == 'delivery' and session['requested_i$
            return redirect(url_for('transaction.get_address'))
        elif order_type.select.data == 'pickup' and session['requested_i$
            return redirect(url_for('transaction.checkout'))

功能很好。该网站按照我想要的方式工作,只是由于某种原因我收到了“不是有效选择”的错误。仅当我使用删除项目或清除购物车按钮之一时才会出现此情况。

最佳答案

我认为问题在于这一行:

if order_type.validate_on_submit() and order_type.submit.data:

在这里,您无条件验证 OrderType 表单,因此每当您单击其他按钮时,该表单的验证都会因选择下拉列表没有值而失败。

尝试颠倒条件句的两个部分,如下所示:

if order_type.submit.data and order_type.validate_on_submit():

这样,只有当该表单的提交按钮有值时才会进行验证,这表明这是用户提交的表单。

关于python - 在 Flask 应用程序中使用 wtforms 选择字段获取 "Not a valid choice",我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33205314/

相关文章:

python - Flask-Babel 的 Flask 测试设置

jquery - 无法使用 Infinite Scroll 和 Flask 进行分页

python - Flask WTForms BooleanField UnboundField

python - WTForms/ flask : Dynamic min_entries

python - 使用 ctypes 在 Python 中读取 C 结构

python - 如何检查日期字段是否为 ISO 格式?

Python。方法 sort 的关键参数中可以使用什么 str.attribute ?设置.排序(键)

python - 我对 Spark 中并行操作的理解是否正确?

python - 使用 flask 时出现"Permission denied"

python - Flask WTForms 在 validate_on_submit() 上总是给出 false