-1
我想刪除數據庫中的1行數據。當我點擊按鈕刪除然後顯示一個對話框來確認。但是,當我使用Ajax我無法刪除。請幫幫我! 這是我的看法代碼如何在python中調用def到ajax
def company_delete(request):
if request.method == 'POST':
company = Company.objects.get(pk=int(QueryDict(request.body).get('postpk')))
company.delete()
response_data = {}
response_data['mgs'] = 'Company was deleted'
return HttpResponse(
json.dumps(response_data),
content_type="application/json"
)
else:
return HttpResponse(
json.dumps({"nothing to see": "this isn't happening"}),
content_type="application/json"
)
這是我的html代碼:
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<a id="opener" type="submit" class="btn btn-danger">Delete</a>
</div>
<div id="dialog" title="Are you sure?">
<p>Some helpful info here</p>
</div>
</div>
這是我的jQuery代碼
$(function() {
$("#dialog").dialog({
autoOpen: false,
resizable: false,
height: 140,
modal: true,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "explode",
duration: 1000
},
buttons: {
"Yes": function delete_company(companyid) {
$.ajax({
type: "POST",
url: "company_delete/",
data: { postpk : companyid },
success: function (json) {
$('#dialog' + companyid).hide();
console.log("company deletion successful");
$(this).dialog("close");
}
});
},
Cancel: function() {
$(this).dialog("close");
}
}
});
$("#opener").click(function() {
$("#dialog").dialog("open");
});
});
這是我的網址:
urlpatterns = [
url(r'^delete/(?P<company_id>\d+)/$', views.company_delete, name='company_delete'),]
@TommyOliver嘗試使用瀏覽器調試器的「網絡」選項卡(F12)查看您的請求是否將其發送到Web服務器。 – Selcuk
url(r'^ delete /(?P \ d +)/ $',views.company_delete,name ='company_delete'), –