How do I populate a select tag with Flask?

Viewed 20275

I am trying to access a list of JSON objects (from mongo) in python to populate a dropdown list using flask templates. Subsequently, I need to access the selected item. I am having difficulty getting the list to populate. Here's my python code and template. Can anyone point me in the right direction? I have not been able to find good documentation on how to populate the dd list with these templates.

PYTHON:

@app.route('/page', methods=["GET", "POST"])
@login_required
def chooser():
    # Option list returns a list of JSON objects 
    option_list = get_options(g.user)
    # {u'_id': ObjectId('52a347343be0b32a070e5f4f'), u'optid': u'52a347343be0b32a070e5f4e'}

    # for debugging, checks out ok
    print option_list

    # Get selected id & return it
    if request.form['submit'] == 'Select':
            optid = o.optid
            resp = 'You chose: ', optid
            return Response(resp)

    return render_template('chooser.html')

HTML TEMPLATE:

{% extends "layout.html" %}
{% block content %}
    <h2>Chooser<h2>
        <h3><table><form action="" method="POST">
          <td>
          <label>Select :</label>
            <select name="option" width="300px">
              {% for o in option_list %}
                <option name="{{ o.optid }}" SELECTED>{{ o.optid }}</option>
              </select>
              </td>
            <td>
              <input class="button1" type="submit" value="Select">
            </td>
              {% endfor %}
        </form></table><h3>

  </div>
{% endblock content %}
1 Answers
Related