【发布时间】:2020-08-05 21:44:15
【问题描述】:
我正在使用烧瓶,我正在努力从下拉菜单中获取数据,我在下拉菜单中从列表“Mydevices”中列出值,因为我使用 Get 方法,我知道我不能同时使用 Get 和 Post 方法,那么当使用选择设备时,如何让我的 Device 变量提交回来?也许使用java脚本?是否有另一种无需编写 java 脚本代码即可获取数据的方法?
<form id="form" action="" method="POST">
<div class="container">
<div class="row">
<a class='dropdown-button btn' href='#' data-activates='dropdown1' data-beloworigin="true">Select Device <img src="/static/pictures/down-arrow.png"></a>
<ul id='dropdown1' class='dropdown-content'>
<li><a name="device" method="GET" SelectDevice="/">
<li><a inputmode="submit" value="{{mydevices[0]}}" selected>{{mydevices[0]}}</a></li>
{% for device in mydevices[1:] %}
<li><a value="{{device}}">{{device}}</a></li>
{% endfor %}
<!-- </li></a> -->
</ul>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12 form-group">
<label class="labeltext">filter Data by:</label><br>
<div class="form-check-inline">
<label class="customradio"><span class="radiotextsty">Days</span>
<input type="radio" checked="checked" name="radio" value="Days">
<span class="checkmark"></span>
</label>
<label class="customradio"><span class="radiotextsty">Months</span>
<input type="radio" name="radio" value="Months">
<span class="checkmark"></span>
</label>
<label class="customradio"><span class="radiotextsty">Seasons</span>
<input type="radio" name="radio" value="Seasons">
<span class="checkmark"></span>
</label>
</div>
</div>
<div class="row">
<button action= {{url_for('index')}} class="col s2 offset-s5 waves-effect waves-light btn" id="sub" type="submit" >Reset</button>
</div></div>
</div>
</div>
</form>
这是我的python代码
@app.route('/device_statistics', methods=['POST', 'GET'])
def device_stats():
mydevices = ['Hall', 'Door101', 'Door102', 'Garage' , 'Fiat 500','Audi A3']
if request.method == 'POST':
chosendevice = request.form['device']
return render_template('device_statistics.html', mydevices=mydevices, error=error)
....
这是我得到的错误:
werkzeug.exceptions.BadRequestKeyError
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'device'
【问题讨论】:
标签: javascript python html flask get