mardi 28 février 2017

keep original selected values after form submit python/flask

I have a two pronged question and would appreciate any advice.

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.

name_list = [p['name'] for p in posts.find({'type': "server"})]
name_list.insert(0, "select")

This is then refernced in my html template (thank you again to the person who helped me with this loop on a previous question)

      <select name="option" id="myselect" onchange="this.form.submit()">
      

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?

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.

Please see full code below:

python:

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)

html/jinja:

<html>
  <head>
    <title> - Test</title>
  </head>
  <body>
    <h1>Hi, !</h1>


      <h2>Database selector</h2>
        <h3><table><form action="" method="post">
          <td>
          <label>Select1 :</label>
            <!--<select name="option" width="300px">-->
              <select name="option" id="myselect" onchange="this.form.submit()">
              

            </select>

          </td>

            </form></table></h3>

        <h3><table><form action="" method="post">
          <td>
            <label>Select2 :</label>
              <select name="option1" id="sub_myselect" onchange="this.form.submit()">
              
          </td>

        </form></table></h3>

  <h3>Here is the choice: </h3>
  <h3>Here is the choice: </h3>
  </body>
</html>

Any pointers or ideas would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire