这个东西看上去很容易 ,但是如果有个小细节把握不好的话,那么根本无法完成,那就是request.get_all()的使用。
以下是我的代码:
在template中很简单:admin/infoList.html
{% for info in infos %}
<form action = "/admin/infos/d" method = "post">
<input type="checkbox" name="id" value='{{ info.key.id }}'>{{ info.key.id }} :name的值就是前台取的值,vaule是真值
{% endfor %}
<input type="submit" name='submit' value="批量删除">
</form>
前台:('/admin/infos/d', Infosd),路径指向的函数 def
class Infosd(webapp.RequestHandler):
def post(self):
infoId = self.request.get_all('id')#这里的get_all是关键,如果用的是get的话他只会一次取一个删除,而且不是List,不能循环
for i in infoId:
info = Info.get_by_id(int(i))
info.delete()
infos = Info.all()
template_values = {
'infos':infos,
'infoId':id
}
path = os.path.join(os.path.dirname(__file__), 'admin/infoList.html')
self.response.out.write(template.render(path, template_values))
JS常用代码:五行搞定checkbox全选/全不选 收藏
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>无标题文档</title>
- <style type="text/css">
- p {margin:0;font-size:12px;line-height:26px;}
- </style>
- <script type="text/javascript">
- function check_all(obj,cName)
- {
- var checkboxs = document.getElementsByName(cName);
- for(var i=0;i<checkboxs.length;i++){checkboxs[i].checked = obj.checked;}
- }
- </script>
- </head>
- <body>
- <p><input type="checkbox" name="all" onclick="check_all(this,'c')" />全选/全不选</p>
- <p><input type="checkbox" name="c" value="" /></p>
- <p><input type="checkbox" name="c" value="" /></p>
- <p><input type="checkbox" name="c" value="" /></p>
- <p><input type="checkbox" name="c" value="" /></p>
- </body>
- </html>