Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从烧瓶中的下拉列表中选择_Python_Mongodb_Web_Flask - Fatal编程技术网

Python 从烧瓶中的下拉列表中选择

Python 从烧瓶中的下拉列表中选择,python,mongodb,web,flask,Python,Mongodb,Web,Flask,我对Flask和web开发非常陌生,在从mongdb查询生成列表并将其传递到html模板以在Flask的下拉菜单中时遇到了一些问题 请参阅下面的当前代码: views.py from flask import render_template from app import app from pymongo import MongoClient @app.route('/') @app.route('/index') def index(): user = {'name': 'Bob'}

我对Flask和web开发非常陌生,在从mongdb查询生成列表并将其传递到html模板以在Flask的下拉菜单中时遇到了一些问题

请参阅下面的当前代码:

views.py

from flask import render_template
from app import app
from pymongo import MongoClient

@app.route('/')
@app.route('/index')

def index():

user = {'name': 'Bob'}

client = MongoClient()

client = MongoClient('mongodb://localhost:27017/')
db = client.test_database
db = client['test-database']

collection = db.test_collection
posts = db.posts
name_list = []

for post in posts.find({'type': "server"}):
    name_list.append(post['name'])

# Get selected option and save into variable?
#if:
#        choice = x.option

return render_template("index.html",
                       title='Database selector',
                       user = 'Bob',
                       server_list=name_list)
服务器列表包含: [u'server1',u'server2',u'server3']

index.html模板

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


      <h2>Option selector</h2>
        <h3><table><form action="" method="POST">
          <td>
          <label>Select :</label>
            <select name="option" width="300px">
            {% for x in server_list %}
            <option value="{{ x.option }}" SELECTED>{{ x.option }}</option>
            {% endfor %}
            </select>
          </td>

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


  </body>
</html>

{{title}}-测试
嗨,{{user}}!
选项选择器
选择:
{%x在服务器\列表%}
{{x.option}}
{%endfor%}
首先,选择列表不会被填充,其次,是否有人对如何捕获选择结果有什么建议,以便我可以在另一个数据库查询中使用它并生成第二个下拉列表

非常感谢您的任何提示


谢谢你

你犯了一个非常简单的错误。我也会给你一些额外的清理建议。最终,您对
x.option
的引用在Jinja语法中是不正确的。因为它是一个简单的列表,所以您应该只使用
x
(它没有
选项
属性)。不要忘记使用endfor和endif子句

Python:

@app.route('/')
@app.route('/index')
def index():
    user = {'name': 'Bob'}

    client = MongoClient('mongodb://localhost:27017/')
    db = client['test-database']

    collection = db.test_collection
    posts = db.posts
    name_list = [p['name'] for p in posts.find({'type': "server"})]

    return render_template("index.html",
                           title='Database selector',
                           user='Bob',
                           server_list=name_list)
{% for x in server_list %}
<option value="{{ x }}"{% if loop.first %} SELECTED{% endif %}>{{ x }}</option>
{% endfor %}
Jinja/HTML:

@app.route('/')
@app.route('/index')
def index():
    user = {'name': 'Bob'}

    client = MongoClient('mongodb://localhost:27017/')
    db = client['test-database']

    collection = db.test_collection
    posts = db.posts
    name_list = [p['name'] for p in posts.find({'type': "server"})]

    return render_template("index.html",
                           title='Database selector',
                           user='Bob',
                           server_list=name_list)
{% for x in server_list %}
<option value="{{ x }}"{% if loop.first %} SELECTED{% endif %}>{{ x }}</option>
{% endfor %}
{服务器中x的%u列表%}
{{x}
{%endfor%}

谢谢你PJ Santoro..-这非常有用-作为一个延续,我希望将选择的结果传递回代码,以进行新的数据库查询,然后在同一页面上填充第二个下拉框。我有类似于python和jinja的代码。。但是,未正确返回提交的值-您是否有进一步的想法?如果请求,表单['submit']='myselect':choice=x sub_name_list=[q['sub_server']for q in posts.find({'type':choice})]返回呈现模板(“index.html”,title='Database selector',user='Bob',server_list=name_list,sub_server_list=sub_name_list)选择2:{sub_server_list%}{{y}}{%endfor%}如果您需要进一步的帮助,我建议您使用一个新的问题,而不是在注释中插入一堆代码。