Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I populate a select tag with Flask?
    text
    copied!<p>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.</p> <p>PYTHON:</p> <pre><code>@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 &amp; return it if request.form['submit'] == 'Select': optid = o.optid resp = 'You chose: ', optid return Response(resp) return render_template('chooser.html') </code></pre> <p>HTML TEMPLATE:</p> <pre><code>{% extends "layout.html" %} {% block content %} &lt;h2&gt;Chooser&lt;h2&gt; &lt;h3&gt;&lt;table&gt;&lt;form action="" method="POST"&gt; &lt;td&gt; &lt;label&gt;Select :&lt;/label&gt; &lt;select name="option" width="300px"&gt; {% for o in option_list %} &lt;option name="{{ o.optid }}" SELECTED&gt;{{ o.optid }}&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt; &lt;input class="button1" type="submit" value="Select"&gt; &lt;/td&gt; {% endfor %} &lt;/form&gt;&lt;/table&gt;&lt;h3&gt; &lt;/div&gt; {% endblock content %} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload