【发布时间】:2018-07-27 23:17:09
【问题描述】:
尝试用烧瓶和 sqlalchemy 做一个简单的待办事项应用程序,因为我以前从未在烧瓶中使用过复选框,所以我遇到了以下问题。
应用是
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(200))
complete = db.Column(db.Boolean)
@app.route('/update', methods=['POST'])
def update():
print(request.form)
return redirect(url_for('index'))
HTML 是:
<ul>
{% for todo in incomplete %}
<li><input id="checkbx" name="{{ todo.id }}" type="checkbox"> {{ todo.text }} </li>
{% endfor %}
</ul>
当我,
print(request.form)
选中复选框完成待办事项并点击更新按钮后,控制台打印出:
ImmutableMultiDict([('11', u'on')])
如何更新数据库以将完整值从“0”更改为“1”?
print(request.form['checkbx'])
给出 400 错误请求
request.form.get('checkbx')
返回无
todo = request.form.getlist('checkbx')
返回 []
我猜我需要做类似的事情:
todo = Todo.query.filter_by(int(id)).first()
所以我可以
todo.complete = True
db.session.commit()
如何从 ImmutableMultiDict 中获取 id(在本例中为“11”)?
【问题讨论】:
-
我专门将 id 从“checkbox”更改为“checkbx”,看看我哪里出错了。添加一个值也不会改变任何事情。 request.form.getlist('checkbox')[1] 继续返回[]
标签: python flask sqlalchemy