首页 > 解决方案 > 如何在烧瓶 wtf 中创建下拉字段

问题描述

我想使用烧瓶 wtforms 创建下拉菜单。我在 bootstrap 4 中找到了下拉菜单,但我无法使用它。我想用烧瓶 wtf 来做。这是我想要实现的目标:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<form class="" action="index.html" method="post">
  <div class="form-group">
    <label for="exampleFormControlSelect1">Example select</label>
    <select class="form-control" id="exampleFormControlSelect1">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
    </select>
  </div>
</form>

标签: pythonflaskdropdownflask-wtforms

解决方案


wtforms提供SelectField

请参阅文档 https://wtforms.readthedocs.io/en/2.3.x/fields/#wtforms.fields.SelectField

示例(来自文档)

class PastebinEntry(Form):
    language = SelectField(u'Programming Language', choices=[('cpp', 'C++'), ('py', 'Python'), ('text', 'Plain Text')])

推荐阅读