我在烧瓶中有一个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添加到模板时,浏览页面将终止应用程序,而不会出现错误,异常或调试消息。如果我省略了SelectField,它将正常工作!知道出了什么问题吗?

更新

刚解决!似乎您需要将选择指定为空列表或形式为[(a,b)](元组列表)的列表以使其起作用。我将代码更改为pytz.common_timezones中x的choices = [(x,x)),它解决了这个问题。

最佳答案

刚解决!似乎您需要将选择指定为空列表或形式为[(a,b)](元组列表)的列表以使其起作用。我将代码更改为

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


它解决了这个问题。

关于python - 将SelectField添加到模板时, flask WTForms终止,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54241707/

10-10 21:31