2017-02-28 117 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中。