使用烧瓶从选择标签获得价值 [英] Getting value from select tag using flask
问题描述
我是Flask的新手,无法从我的选择标签获取值。我已经尝试了 request.form ['comp_select']
,它返回一个错误的请求。然而,当我尝试使用 request.form.get('comp_select')
时,我的返回页面返回一个空白列表[]。
我的html:
$ b
< ; form class =form-inlineaction ={{url_for('test')}}>
< div class =form-group>
< div class =input-group>
< span class =input-group-addon>请选择< / span>
{%for o in data%}
{%endfor%}
< / select>
< / div>
< button type =submitclass =btn btn-default> Go< / button>
< / div>
< / form>
我的app.py:
@ app.route(/ test,methods = ['GET','POST'])
def test():
select = request.form.get('comp_select')
return(str(select))#只是看看是什么选择
如果我的格式是关闭的(对Stack Overflow来说也是新的),那么提前抱歉。
很难从你提供的内容中知道确切的内容,但是我相信你需要在> method =POST
< form> 元素。
从 flask doc用于请求
对象:
lockquote
要访问表单数据(通过POST或PUT请求传输的数据),可以使用表单属性。 ...要访问在URL(?键=值)中提交的参数,你可以使用args属性。
所以,如果你通过POST提交表单,使用 request.form.get()
。如果您通过GET提交表单,请使用 request.args.get()
。
你希望它:
flask_app.py:
#! / usr / bin / env python $ b $ from flask flask Flask,flash,redirect,render_template,\
request,url_for
$ b 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():$ b $ select = request.form.get('comp_select')
return(str(select))# b
$ b if __name __ =='__ main__':
app.run(debug = True)
templates / index.html
< form class =form-inlinemethod =POST action ={{url_for('test')}}>
< div class =form-group>
< div class =input-group>
< span class =input-group-addon>请选择< / span>
{%for o in data%}
{%endfor%}
< / select>
< / div>
< button type =submitclass =btn btn-default> Go< / button>
< / div>
< / form>
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 "[]".
My 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.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).
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.
From the flask doc for the request
object:
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.
So, if you submit your forms via POST, use request.form.get()
. If you submit your forms via GET, use request.args.get()
.
This app behaves the way you want it to:
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
<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>
这篇关于使用烧瓶从选择标签获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!