Django - ajax调用条件方法

时间:2017-06-11 14:44:09

标签: ajax django

如果条件为真,我想通过ajax调用删除记录。但是我的ajax总是将它删除。怎么解决呢?

def delete_order(request, pk, user=None):
    try:
        if request.user == 'owner':
            order = Food.objects.get(food_name=order.order_name).price
            latestCredit = Credit.objects.filter(user = request.user).last().creditAmount
            nextCredit = float(latestCredit) + float(orderPrice)
            q = Credit(user = request.user, creditAmount = nextCredit, status='1')
            q.save()
            order.delete()
            return HttpResponse("removed")
        else:
            return HttpResponse("you cant removed")

    except Exception:

        return HttpResponse("error occured")

AJAX

$('.delete').click(function(){
    url_delete = 'my-url.com;
    $.ajax({
        type: 'GET',
        url: url_delete,
        data:$(".delete_reserv").serialize(),
        success:function(data) {

            $row.remove();

        }
    });
});

1 个答案:

答案 0 :(得分:1)

您可以尝试在ajax调用中检查响应文本:

$('.delete').click(function(){
    url_delete = 'my-url.com;
    $.ajax({
        type: 'GET',
        url: url_delete,
        data:$(".delete_reserv").serialize(),
        success:function(data) {
            if(data == 'removed') {
                $row.remove();
            }
        }
    });
});