Python 使用烧瓶从选择标签中获取价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32019733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:53:04  来源:igfitidea点击:

Getting value from select tag using flask

pythonhtmlflask

提问by qwertyuip9

I'm new to Flask and I'm having trouble getting the value from my select tag. I have tried request.form['comp_select']which returns a Bad Request. However, when I try using request.form.get('comp_select'), my return page returns a blank list "[]".

我是 Flask 的新手,无法从 select 标签中获取值。我试过request.form['comp_select']它返回一个错误的请求。但是,当我尝试使用 时request.form.get('comp_select'),我的返回页返回一个空白列表“[]”。

My html:

我的html:

<form class="form-inline" 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>

My app.py:

我的应用程序.py:

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

Sorry in advance if my formatting is off for the post (also new to Stack Overflow).

如果我的帖子格式已关闭,请提前抱歉(也是 Stack Overflow 的新手)。

采纳答案by Rob?

It's hard to know for certain from what you've provided, but I believe you need to add method="POST"to your <form>element.

从您提供的内容中很难确定,但我相信您需要添加method="POST"到您的<form>元素中。

From the flask doc for the requestobject:

对象烧瓶文档request

To access form data (data transmitted in a POST or PUT request) you can use the form attribute. ... To access parameters submitted in the URL (?key=value) you can use the args attribute.

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

So, if you submit your forms via POST, use request.form.get(). If you submit your forms via GET, use request.args.get().

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

This app behaves the way you want it to:

这个应用程序按照您希望的方式运行:

flask_app.py:

烧瓶应用程序.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)

templates/index.html

模板/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>