Ajax: post array of integers to Django

后端 未结 2 785
[愿得一人]
[愿得一人] 2021-01-06 15:35

I\'m using DataTables. I want to let the user select multiple rows and delete them. So far I have it working so it deletes the first row in the selection using the code belo

2条回答
  •  我在风中等你
    2021-01-06 16:06

    You could try sometings like this:

    $(function(){
        $.post("{% url delete %}", {"ids[]": anSelected}, function(res){
        if(res.ok){
            // remove rows from your table, maybe :)
            // oTable.fnDeleteRow(anSelected);
          }else{
            alert(res.errors); 
          } 
        });
    })
    

    On the server:

    @ajax_request
    def test(request):
        ids = request.POST.getlist("ids[]")
        try:
            Items.objects.filter(id__in=ids).delete()
        except:
            return {"ok": False, "errors": "your error"}
        return {"ok": True}
    

    the @ajax_request decorators is from https://bitbucket.org/offline/django-annoying/wiki/Home and let you return json response.

提交回复
热议问题