【问题标题】:I'm trying to create a drop down list using flask and html, but there's something wrong. Can someone help me out?我正在尝试使用烧瓶和 html 创建一个下拉列表,但是有问题。有人可以帮我吗?
【发布时间】:2020-09-15 13:14:21
【问题描述】:
    {{ form.product.label }}
    <select name="{{ form.product.name }}" id="product">
         {% for product in products %}
              <option value="{{product.name}}">{{product.name}}</option>
         {% endfor %}
    </select>

我试图让用户从产品表中的名称中选择一个值并将其存储在另一个表中,但下拉列表是空的,而它应该有五个选项。

  movements = Movement.query.all()
  products = Product.query.all()
  locations = Location.query.all()
  return render_template('movements.html', movements=movements, products=products, locations=locations, form=form)

【问题讨论】:

  • 你还没有告诉我们什么是行不通的。你得到了什么输出?
  • 下拉列表是空的,它应该有五个选项。
  • 您确定从您的产品查询中收到任何信息吗?
  • 当我检查页面源代码时,选择标签中没有任何内容,所以我认为我不是,但我不知道为什么不这样做,因为我在另一个中使用了相同的 for 循环程序的一部分(打印值,而不是创建列表),并且有效。
  • 无论products 变量是否为空,您是否尝试过在路由函数中调试它?

标签: html flask sqlalchemy


【解决方案1】:

我发现出了什么问题,表单中的产品字段是StringField,而不是SelectField。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多