title: keep original selected values after form submit python/flask
body: <p>I have a two pronged question and would appreciate any advice.</p>
  
  <p>1) I have a flask template with multiple forms in it. Each form presents the user with a list that is dynamically generated from a mongodb query.</p>
  
  <pre><code>name_list = [p['name'] for p in posts.find({'type': "server"})]
  name_list.insert(0, "select")
  </code></pre>
  
  <p>This is then refernced in my html template (thank you again to the person who helped me with this loop on a previous question)</p>
  
  <pre><code>      &lt;select name="option" id="myselect" onchange="this.form.submit()"&gt;
        {% for x in server_list %}
        &lt;option value="{{ x }}"{% if loop.first %} SELECTED{% endif %}&gt;{{ x }}&lt;/option&gt;
        {% endfor %}
  </code></pre>
  
  <p>This selection is then passed back to python to be used in another db query and then present the user with another dropdown select box. However when the first form is submitted, the new page render means that that value is now lost on the html and appears blank. Is there a way to keep this choice persistent so when after the new page render it still appears?</p>
  
  <p>2) Secondly, I would like to keep a running list of what options the user makes, however given the if, elif structure I'm using that variable looses state and can no longer used. Eventually I would like to present the user with two sets of these dropdown menus to generate to final db querys that I can compare and return the difference, however I can only do this if I can keep state of these values generated in the loops.</p>
  
  <p>Please see full code below:</p>
  
  <p>python:</p>
  
  <pre><code>from flask import render_template
  from flask import request
  from flask import Response
  from app import app
  from pymongo import MongoClient
  
  @app.route('/', methods=['POST','GET'])
  @app.route('/index', methods=['POST','GET'])
  
  def index():
      user = {'name': 'Bob'}
  
      client = MongoClient('mongodb://localhost:27017/')
      db = client['test-database']
  collection = db.test_collection
  name_list = []
  posts = db.posts
  name_list = [p['name'] for p in posts.find({'type': "server"})]
  name_list.insert(0, "select")
  select_list = []
  
  #if request.form['submit'] == 'myselect':
  if request.method == 'POST' and request.form.get('option'):
      choice = request.form.get('option')
      select_list.append(choice)
      sub_name_list = [q['type'] for q in posts.find({'name': choice})]
      return render_template("index.html",
                      sub_server_list=sub_name_list)
  
  
  elif request.method == 'POST' and request.form.get('option1'):
      choice1 = request.form.get('option1')
      select_list.append(choice1)
      return render_template("index.html",
                      title='Database selector',
                      user='Person',
                      choiced=choice1,
                      total_choice=select_list)
  
  
  return render_template("index.html",
                         title='Database selector',
                         user='Person',
                         server_list=name_list)
  </code></pre>
  
  <p>html/jinja:</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;Database selector&lt;/h2&gt;
          &lt;h3&gt;&lt;table&gt;&lt;form action="" method="post"&gt;
            &lt;td&gt;
            &lt;label&gt;Select1 :&lt;/label&gt;
              &lt;!--&lt;select name="option" width="300px"&gt;--&gt;
                &lt;select name="option" id="myselect" onchange="this.form.submit()"&gt;
                {% for x in server_list %}
                &lt;option value="{{ x }}"{% if loop.first %} SELECTED{% endif %}&gt;{{ x }}&lt;/option&gt;
                {% endfor %}
  
              &lt;/select&gt;
  
            &lt;/td&gt;
  
              &lt;/form&gt;&lt;/table&gt;&lt;/h3&gt;
  
          &lt;h3&gt;&lt;table&gt;&lt;form action="" method="post"&gt;
            &lt;td&gt;
              &lt;label&gt;Select2 :&lt;/label&gt;
                &lt;select name="option1" id="sub_myselect" onchange="this.form.submit()"&gt;
                {% for y in sub_server_list %}
                &lt;option value="{{ y }}"{% if loop.first %} SELECTED{% endif %}&gt;{{ y }}&lt;/option&gt;
                {% endfor %}
            &lt;/td&gt;
  
          &lt;/form&gt;&lt;/table&gt;&lt;/h3&gt;
  
    &lt;h3&gt;Here is the choice: {{ choiced }}&lt;/h3&gt;
    &lt;h3&gt;Here is the choice: {{ total_choice }}&lt;/h3&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Any pointers or ideas would be greatly appreciated.</p>