当选择字段添加到模板时,烧瓶 WTF 表单终止



我在烧瓶中有一个 wtf 形式:


class CustomerForm(FlaskForm):
    customer_id = StringField('System ID')
    crm_id = IntegerField('ID', validators=[DataRequired()])
    customer_name = StringField('Customer Name', validators=[DataRequired()])
    alias = StringField('Alias', validators=[DataRequired()])
    phone = StringField('Phone')
    address = StringField('Address')
    default_timezone = SelectField('Default Time Zone', validators=[DataRequired()],
        choices=pytz.common_timezones
    )
@customers_page.route('/customer/<customer_id>', methods=["GET"])
@customers_page.route('/customer/', defaults={'customer_id': '-1'}, methods=["GET"])
@flask_login.login_required
def customer_page(customer_id):
    form = CustomerForm()
    return render_template('customer.html', 
        form=form
    )

模板如下所示:

{% extends "main.html" %}
{% block content %}
<div class="app-content">
    <h1>View/Edit/Delete Customer {{ customer.customer_id }}</h1>
    <form action="{{ url_for('customers_page.customer_save') }}" method="post">
        {{ form.crm_id(readonly=true) }}
        {{ form.customer_name }}
        {{ form.alias }}
        {{ form.phone }}
        {{ form.address }}
        {{ form.default_timezone }}
        {{ form.csrf_token }}
    </form>
</div>
{% endblock %}

当我将 SelectField form.default_timezone添加到模板时,浏览页面会终止应用程序,而不会显示错误、异常或调试消息。如果我省略选择字段,它可以正常工作!知道出了什么问题吗?

更新

刚刚解决!似乎您需要将选项指定为空列表或格式为 [(a, b)](元组列表)的列表才能使其工作。我将代码更改为 choices=[(x, x) for x in pytz.common_timezones] 并解决了这个问题。

刚刚解决!似乎您需要将选项指定为空列表或格式为 [(a, b)](元组列表)的列表才能使其工作。我将代码更改为类似

default_timezone = SelectField('Default Time Zone', validators=[DataRequired()], 
          choices=[(x, x) for x in pytz.common_timezones])

它解决了问题。

最新更新