奇怪的HTML选择标签行为与python烧瓶

时间:2018-04-14 16:29:13

标签: python html flask

带数据绑定的HTML

 Location:<br>
  {% for item in data %}
   <select name="location">
       <option value={{ item[0] }}>{{ item[0] }}</option>
  </select>
  {% endfor %}

后端

@app.route('/events', methods = ['post', 'get'])
def events():
    #data = ['loc1','loc2','loc3']
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM location')
    rows = cursor.fetchall()
    data = [row for row in rows]
    cursor.close()
    return render_template('events.html', data = data)

当我render_template并传入数据时,它会从数据库中检索locations的列表,并将其作为选项进行选择。

现在看起来像这样

enter image description here

它应该在选择列表(?)上有4个选项,但是只有4个单独的选择列表,其中包含on选项。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您需要循环选择选项,而不是整个选择块:

Location:<br>
  <select name="location">
   {% for item in data %}
     <option value={{ item[0] }}>{{ item[0] }}</option>
   {% endfor %}
 </select>