SpeedyH30
SpeedyH30

Reputation: 89

django delete() success function error

So Im having issues with my delete function, it does delete the object its meant to but wont go to window.location. Instead I get the error

DoesNotExist at /api/personnel/delete/ Resource matching query does not exist.

Which I imagine is because it has just been deleted. how do I get past this issue?

var deletepers = function(){    
var persid = getUrlVars()["id"];
data={persid}
console.log(persid);
        $.ajax({
        type: "POST",
        url: "/api/personnel/delete/",
        data: JSON.stringify(data),
        contentType: "application/json",
        dataType: 'json',
            success:function(response){
                window.location.href = "/Personnel";
        }   
    })
}

def delete_personnel(request):

    # Try find the requested app
    if request.method == "POST":
    pers_id = request.POST.get('persid')

    pers = Resource.objects.get(id=pers_id)
    if not pers: return HttpResponseNotFound()

    pers.delete()

    return HttpResponse(content_type='application/json')

Upvotes: 1

Views: 411

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599470

You aren't passing the data in the format that your view is expecting. {persid} in JS is interpreted just as persid, not a hash at all; so in the view, request.POST.get('persid') is None.

Instead use an actual JS hash:

data = { persid: persid }

Upvotes: 1

Related Questions