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

时间:2022-11-27 17:05:27

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'],它返回一个Bad Request。但是,当我尝试使用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:

我的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

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

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

1 个解决方案

#1


14  

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”。

From the flask doc for the request object:

从flask文档中获取请求对象:

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请求中传输的数据),您可以使用表单属性。 ...要访问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:

flask_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)

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>

#1


14  

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”。

From the flask doc for the request object:

从flask文档中获取请求对象:

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请求中传输的数据),您可以使用表单属性。 ...要访问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:

flask_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)

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>