Django 实现批量删除

首先把 input 标签放入 for 循环里面

然后个删除连接
删除

然后用Ajax来异步函数:
代码如下:
ajax:
function dele(){
var dele = $’(".c:checked")
var clist = []
KaTeX parse error: Expected '}', got 'EOF' at end of input: … clist.push((this).val())
})
console.log(clist)
$.ajax({
url:’/dele/’,
type:‘post’,
data:{‘clist’:clist.join(’,’)},
success:function(res){
var res = JSON.parse(res)
if(res.code == 200){
alert(res.mes)
window.location.reload();
}
}
})
}

视图接口:

def dele(request):
if request.method==‘POST’:
mes ={}
clist = request.POST.get(‘clist’)
clist = clist.split(’,’)
for i in clist:
good = models.Good.objects.get(id=int(i))
good.delete()
mes[‘code’] = 200
mes[‘mes’] = ‘删除成功’
mes = json.dumps(mes,ensure_ascii=False)
return HttpResponse(mes)

最后路由配置 :

你可能感兴趣的:(Django 实现批量删除)