title: Select from dropdown list in flask
body: <p>I'm very new to Flask and web development and am having some trouble with generating a list from a mongdb query and passing that through to the html template to be in a dropdown menu in Flask.</p>
  
  <p>Please see the current code below:</p>
  
  <p>views.py</p>
  
  <pre><code>from flask import render_template
  from app import app
  from pymongo import MongoClient
  
  @app.route('/')
  @app.route('/index')
  
  def index():
  
  user = {'name': 'Bob'}
  
  client = MongoClient()
  
  client = MongoClient('mongodb://localhost:27017/')
  db = client.test_database
  db = client['test-database']
  
  collection = db.test_collection
  posts = db.posts
  name_list = []
  
  for post in posts.find({'type': "server"}):
      name_list.append(post['name'])
  
  # Get selected option and save into variable?
  #if:
  #        choice = x.option
  
  return render_template("index.html",
                         title='Database selector',
                         user = 'Bob',
                         server_list=name_list)
  </code></pre>
  
  <p>server_list contains: 
  [u'server1', u'server2', u'server3']</p>
  
  <p>index.html template</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;{{ title }} - Test&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;h1&gt;Hi, {{ user }}!&lt;/h1&gt;
  
  
        &lt;h2&gt;Option selector&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 x in server_list %}
              &lt;option value="{{ x.option }}" SELECTED&gt;{{ x.option }}&lt;/option&gt;
              {% endfor %}
              &lt;/select&gt;
            &lt;/td&gt;
  
          &lt;/form&gt;&lt;/table&gt;&lt;/h3&gt;
  
  
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Firstly, the select list does not get populated and secondly, would anyone have a kind suggestions on how to capture the result of the selection so I can use this in another database query and generate a second dropdown list?</p>
  
  <p>Any hints greatly appreciated.</p>
  
  <p>Thank you</p>