2017-06-11 56 views
0

我想通過ajax調用刪除記錄,條件爲true時。但我的ajax總是刪除它。如何解決它?Django - 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

你可以嘗試爲您在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(); 
      } 
     } 
    }); 
});