Dawn17
Dawn17

Reputation: 8297

Weird HTML select tag behavior with python flask

HTML with data-binding

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

Backend

@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)

when I render_template and pass in the data, it retrieves a list of locations from the database and puts it as options to select from.

This now looks like this

enter image description here

It's supposed to have on selection list(?) with 4 options, but there are just 4 separate selection list with on option in it.

How can I fix this?

Upvotes: 0

Views: 93

Answers (1)

Joost
Joost

Reputation: 3729

You need to loop over the select options, not over the entire select block:

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

Upvotes: 2

Related Questions