我的Django / Ajax删除功能有问题。我有一个列表模板,其中每个对象都有一个删除按钮,它将ajax发布到href =" / delete / {{some.id}}"。
查看:
@csrf_exempt
@login_required
def Deleteobject(request, id):
someobject = get_object_or_404(Mymodel, pk=id)
response = {}
if someobject.user == request.user:
someobject.delete()
response["status"] = "Delete"
else:
response["status"] = "Notdelete"
return HttpResponse(json.dumps(response), content_type='application/json')
这里有什么不对? "删除"本身似乎有效。但是window.confirm非常奇怪,我有时需要多次单击“确定”/“取消”。我希望从列表中删除已删除的div的Ajax成功响应根本不起作用。
模板:
$("#contentdiv").on("click", ".deletebutton", function(event) {
event.preventDefault();
var val = confirm('Are you sure you want to delete this?');
if(val == true) {
$.ajax({
url: $(this).attr("href"),
//data: data,
type: 'get',
success: function(response) {
var res = $.parseJSON(response);
if (res['status'] == 'Delete') {
$( ".thelist" ).remove( "#listid" + $(this).attr("id") );
alert('successful!');
}
else if (res['status'] == 'Notdelete') {
delete res['status']
alert('error!');
}
}
});
}
});
编辑:在删除功能上忘记# EDIT2:改编自" post"到"得到" ..
URL:
url(r'^delete/(?P<id>\d+)/$','mymodels.views.Deleteobject'),
答案 0 :(得分:0)
您需要将id发送到post方法,
这里的id会像:
id = request.POST.get ['id'] 然后你可以传递id,你也可以这样传递它:
$("#contentdiv").on("click", ".deletebutton", function(event) {
event.preventDefault();
var val = confirm('Are you sure you want to delete this?');
if(val == true) {
$.ajax({
url: $(this).attr("href"),
//data: data,
type: 'get',
success: function(response) {
var res = $.parseJSON(response);
if (res['status'] == 'Delete') {
$( ".thelist" ).remove( "#listid" + $(this).attr("id") );
alert('successful!');
}
else if (res['status'] == 'Notdelete') {
delete res['status']
alert('error!');
}
}
});
}
编辑:在删除功能上忘记#..