我对表格中的复选框有疑问
<form id="form" action="/findPkgInstalled" role="form" method = "POST">
<div class="input-group col-xs-4">
<input type="text" name="pkgSearch" placeholder="Ricerca applicazione non installate..">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
<h6><input type="checkbox" name="filterName" checked>Filtra per nome</h6>
</form>
在Python中,我有:
@app.route('/findPkgInstalled', methods=['POST'])
def findPkgInstalled():
error = None
pkg = request.form['pkgSearch']
if not pkg:
flash(u'Operazione errata, impossibile ricercare stringa vuota','warning')
return redirect(url_for('listInstalled'))
else:
if request.form['filterName'] is 'on':
appFound = aptsearch(pkg,True)
return render_template('find-pkg-not-installed.html', appFound = appFound)
else:
appFound = aptsearch(pkg,False)
return render_template('find-pkg-not-installed.html', appFound = appFound)
return redirect(url_for('listInstalled'))
box = request.form['filterName']
但这不起作用。报告的错误是400 Bad Request。我能怎么做?你能帮我吗?
答案 0 :(得分:0)
此错误表示您正在尝试使用无效密钥从发布请求中获取对象。例如,如果您取消选中filterName
复选框-这将导致此错误:
request.form['filterName']
我的建议:
0)始终检查您的帖子正文,以了解可以使用哪些键从该正文中检索值。
1)使用
request.form.get('filterName')
代替
request.form['filterName']
因为.get()
返回None
是因为没有这样的键,而不是在烧瓶内抛出导致400错误的异常
2)使用
request.form.get('filterName') == 'on'
代替
request.form['filterName'] is 'on'
因为is
如果两个变量指向内存中的同一对象,则返回True。我不确定进程内存中是否已经有“ on”对象