2017-02-28 396 views
1

我有一個雙管齊下的問題,並希望有任何建議。在表單提交後保留原始選定的值python/flask

1)我有一個有多種形式的燒瓶模板。每個表單都向用戶顯示一個從mongodb查詢動態生成的列表。

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

然後,這是我的HTML模板refernced(再次感謝你給誰用這個循環幫我對前一個問題的人)

 <select name="option" id="myselect" onchange="this.form.submit()"> 
     {% for x in server_list %} 
     <option value="{{ x }}"{% if loop.first %} SELECTED{% endif %}>{{ x }}</option> 
     {% endfor %} 

這種選擇。然後傳回蟒蛇是在另一個數據庫查詢中使用,然後向用戶提供另一個下拉選擇框。但是,當提交第一個表單時,新的頁面呈現意味着該值現在在html上丟失並且顯示爲空白。有沒有辦法保持這種選擇的持久性,以便在新頁面渲染之後它仍然出現?其次,我想保留一個用戶所做的選項的運行列表,但是如果我使用的是elif結構,那麼這個變量就會失去狀態,不能再使用。最終,我想向用戶展示兩組這些下拉菜單,以生成最終的數據庫查詢,我可以比較並返回差異,但是我只能這樣做,如果我可以保留這些循環中生成的值的狀態。

請參考下面全碼:

蟒蛇:

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 /神社:

<html> 
    <head> 
    <title>{{ title }} - Test</title> 
    </head> 
    <body> 
    <h1>Hi, {{ user }}!</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()"> 
       {% for x in server_list %} 
       <option value="{{ x }}"{% if loop.first %} SELECTED{% endif %}>{{ x }}</option> 
       {% endfor %} 

      </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()"> 
       {% for y in sub_server_list %} 
       <option value="{{ y }}"{% if loop.first %} SELECTED{% endif %}>{{ y }}</option> 
       {% endfor %} 
      </td> 

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

    <h3>Here is the choice: {{ choiced }}</h3> 
    <h3>Here is the choice: {{ total_choice }}</h3> 
    </body> 
</html> 

任何指針或想法將不勝感激。

回答

0
  1. 看一看sessions。您可能需要更改存儲數據的方式,可能需要爲choicechoice1找到合適的名稱並將它們存儲在dict中。