我正在创建一个烧瓶窗体,我需要根据Flask中的其他下拉菜单选择字段显示一个下拉菜单。我可以用HTML做到这一点,但是发现用Flask形式很难做到这一点。
routes.py:
class RegistrationForm(FlaskForm):
category = SelectField('Category', choices = [('Clothes', 'Clothes'), ('Watch', 'Watch')])
subcategory = SelectField('Sub Category', choices = [('USPA', 'USPA'), ('LEE', 'LEE'), ('FOSSIL', 'FOSSIL'), ('TITAN', 'TITAN')])
submit = SubmitField('Register')
HTML:
<form action="" method="post">
{{ form.hidden_tag() }}
<p>
<p>
{{ form.category.label }}<br>
{{ form.category }}<br>
{% for error in form.category.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.subcategory.label }}<br>
{{ form.subcategory }}<br>
{% for error in form.subcategory.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>{{ form.submit() }}</p>
</form>
我要映射此链接:
衣服:USPA,李
手表:化石,泰坦
但是以表格的形式我得到了所有选择。我需要基于所选类别的子类别。
由于这是客户端的动态功能,您将需要使用Javascript。
我个人认为,最简单的方法是预先静态配置烧瓶形式:
class RegistrationForm(FlaskForm):
category = SelectField('Category', choices = [('Clothes', 'Clothes'), ('Watch', 'Watch')])
subcategory_clothes = SelectField('Sub Category', choices = [('USPA', 'USPA'), ('LEE', 'LEE')], validators=[Optional()])
subcategory_watches = SelectField('Sub Category', choices = [('Titan', 'Titan'), ('Fossil', 'Fossil')], validators=[Optional()])
submit = SubmitField('Register')
然后使用JavaScript if语句显示一个或另一个组合框,该组合框取决于初始组合框的值。您将需要一个javascript事件挂钩来检测类别的更改,或使用诸如Vue.js的框架