Javascript 表单提交python/flask后保留原始选定值
我有一个双管齐下的问题,希望你能给我一些建议 1) 我有一个烧瓶模板,里面有多种形式。每个表单向用户提供一个从mongodb查询动态生成的列表Javascript 表单提交python/flask后保留原始选定值,javascript,python,web,flask,Javascript,Python,Web,Flask,我有一个双管齐下的问题,希望你能给我一些建议 1) 我有一个烧瓶模板,里面有多种形式。每个表单向用户提供一个从mongodb查询动态生成的列表 name_list = [p['name'] for p in posts.find({'type': "server"})] name_list.insert(0, "select") 这将在我的html模板中引用(再次感谢在上一个问题上帮助我完成此循环的人) html/jinja: <html> <head> &
name_list = [p['name'] for p in posts.find({'type': "server"})]
name_list.insert(0, "select")
这将在我的html模板中引用(再次感谢在上一个问题上帮助我完成此循环的人)
html/jinja:
<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>
{{title}}-测试
嗨,{{user}}!
数据库选择器
选择1:
{%x在服务器\列表%}
{{x}
{%endfor%}
选择2:
{sub_server_list%}
{{y}
{%endfor%}
下面是选择:{{choiced}
这是一个选择:{{total_choice}}
任何建议或想法都将不胜感激
选项
和选项1找到一个合适的名称,并将它们存储在一个文件夹中
选项
和选项1找到一个合适的名称,并将它们存储在一个文件夹中
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>
<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>