Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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 使用flask从select标记获取值_Python_Html_Flask - Fatal编程技术网

Python 使用flask从select标记获取值

Python 使用flask从select标记获取值,python,html,flask,Python,Html,Flask,我是Flask的新手,从select标记获取值时遇到问题。我尝试了请求。返回错误请求的表单['comp_select']。但是,当我尝试使用request.form.get('comp_select')时,我的返回页面返回一个空白列表“[]” 我的html: 请选择 {数据%中的o为%0} {{o.name}} {%endfor%} 去 我的app.py: @app.route("/test" , methods=['GET', 'POST']) def test(): select

我是Flask的新手,从select标记获取值时遇到问题。我尝试了
请求。返回错误请求的表单['comp_select']
。但是,当我尝试使用
request.form.get('comp_select')
时,我的返回页面返回一个空白列表“[]”

我的html:


请选择
{数据%中的o为%0}
{{o.name}}
{%endfor%}
去
我的app.py:

@app.route("/test" , methods=['GET', 'POST'])
def test():
    select = request.form.get('comp_select')
    return(str(select)) # just to see what select is

如果我的文章格式设置已关闭(也是新出现的堆栈溢出),请提前道歉。

很难从您提供的内容中确定,但我相信您需要将
method=“post”
添加到
元素中

从:

要访问表单数据(POST或PUT请求中传输的数据),可以使用表单属性。。。要访问URL中提交的参数(?key=value),可以使用args属性

因此,如果您通过POST提交表单,请使用
request.form.get()
。如果您通过GET提交表单,请使用
request.args.GET()

此应用程序的行为符合您的要求:

烧瓶_app.py:

#!/usr/bin/env python
from flask import Flask, flash, redirect, render_template, \
     request, url_for

app = Flask(__name__)

@app.route('/')
def index():
    return render_template(
        'index.html',
        data=[{'name':'red'}, {'name':'green'}, {'name':'blue'}])

@app.route("/test" , methods=['GET', 'POST'])
def test():
    select = request.form.get('comp_select')
    return(str(select)) # just to see what select is

if __name__=='__main__':
    app.run(debug=True)
模板/index.html

<form class="form-inline" method="POST" action="{{ url_for('test') }}">
  <div class="form-group">
    <div class="input-group">
        <span class="input-group-addon">Please select</span>
            <select name="comp_select" class="selectpicker form-control">
              {% for o in data %}
              <option value="{{ o.name }}">{{ o.name }}</option>
              {% endfor %}
            </select>
    </div>
    <button type="submit" class="btn btn-default">Go</button>
  </div>
</form>

请选择
{数据%中的o为%0}
{{o.name}}
{%endfor%}
去

我遇到了一个问题:是否获取
的所有值选择多个
get
将只返回第一个。答案已经给出;可以使用
request.getlist