遍历 Flask 中的复选框
我有一个 Jinja2 模板,如下所示:
<form action="" method=post>
<table>
<tr>
<th></th>
<th>ID</th>
<th>Title</th>
</tr>
{% for page in pages %}
<tr>
<td><input type=checkbox name=do_delete value="{{ page['id'] }}"></td>
<td>{{ page['id'] }}</td>
<td><a href="{{ page['id'] }}">{{ page['title'] }}</a></td>
</tr>
{% endfor %}
</table>
With selected:
<input type=submit value=Delete>
</form>
我有一个函数,当单击“删除”按钮时,应该根据选中的复选框删除页面:
db.session.query(Page).filter(Page.id.in_(page_ids)).delete()
我所困惑的是如何迭代所有页面复选框并形成已选中的 page_ids
列表。
I have a Jinja2 template that looks like this:
<form action="" method=post>
<table>
<tr>
<th></th>
<th>ID</th>
<th>Title</th>
</tr>
{% for page in pages %}
<tr>
<td><input type=checkbox name=do_delete value="{{ page['id'] }}"></td>
<td>{{ page['id'] }}</td>
<td><a href="{{ page['id'] }}">{{ page['title'] }}</a></td>
</tr>
{% endfor %}
</table>
With selected:
<input type=submit value=Delete>
</form>
And I have a function, that should delete the pages according to which checkboxes were checked, when the 'Delete' button is clicked:
db.session.query(Page).filter(Page.id.in_(page_ids)).delete()
What I'm stuck with is how do I iterate through all the checkboxes and form the page_ids
list of checked ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Flask 的
request
对象(实际上是LocalProxy
实例返回的类,即request
)是 werkzeug 的MultiDict
数据结构 - 其中包括getlist
方法。Flask's
request
object (well, actually the class that is returned by theLocalProxy
instance that isrequest
) is a subclass of werkzeug'sMultiDict
data structure - which includes agetlist
method.