我在一页上使用带有几种形式的flask / flask-wtf。我的app.py代码语句使用前两个if语句,它们是一个表单的一部分,但在第3个/第4个if语句中失败并出现400错误。如果我在视图中交换它们,它们将起作用。此外我的标签似乎在stackoverflow上关闭,但它在我的ide上很好。
app.py
if request.method == 'POST':
###delete
delete = request.form["add"] == 'Delete'
if delete:
print "here at add elete"
#One
selectedOne = request.form.getlist('selectAddressOne')
any_selected = bool(selectedOne)
if any_selected is True:
wallet.autobtcaddress01 = ''
db.session.add(wallet)
db.session.commit()
return redirect(url_for('wallet.walletPageAutoWithdraw', username=user.username))
Add = request.form["add"] == 'Add'
if Add:
selectedOne = request.form.getlist('selectAddressOne')
any_selected = bool(selectedOne)
if any_selected is True:
print "here"
wallet.autobtcaddress01 = form.addressOne.data
db.session.add(wallet)
db.session.commit()
return redirect(url_for('wallet.walletPageAutoWithdraw', username=user.username))
abortOn= request.form.getlist["Abort"] == "On"
if abortOn:
print "abort on"
wallet.autostatus = "Active"
db.session.add(wallet)
db.session.commit()
return redirect(url_for('wallet.walletPageAutoWithdraw', username=user.username))
Abortoff= request.form.getlist["Abort"] == "Abort"
if Abortoff:
wallet.autostatus = "Off"
db.session.add(wallet)
db.session.commit()
return redirect(url_for('wallet.walletPageAutoWithdraw', username=user.username))
HTML
<div id="TurnonorOff">
<form method=POST action="">
<input class="btn btn-default" type=submit value="On" name="Abort">
<input class="btn btn-danger" type=submit value="Abort" name="Abort">
</form>
</div>
<div id="Form5Checkbox">
{{ render_field(form.selectAddressFive, prefs='no')}}
</div>
<div id="Form5">
{{ render_field(form.addressFive,value=wallet.btcaddress05,
autocomplete="off", type="text", size='50') }}
<div id="TurnonorOff">
<form method=POST action="">
<input class="btn btn-default" type=submit value="On" name="Abort">
<input class="btn btn-danger" type=submit value="Abort" name="Abort">
</form>
</div>
<div id="WalletAutoButtonList">
<input class="btn btn-primary" type=submit value="Add" name="add">
<input class="btn btn-danger" type=submit value="Delete" name="add">
</div>
</div>
</form>
我确实遗漏了一些html / python代码,因为我认为它是不必要的冗长和不相关的代码。
答案 0 :(得分:0)
经过仔细比较后,我意识到这是个问题:
abortOn= request.form.getlist["Abort"] == "On"
Abortoff= request.form.getlist["Abort"] == "Abort"
我把它改为:
abortOn= request.form.get["Abort"] == "On"
Abortoff= request.form.get["Abort"] == "Abort"