Skip to content

Instantly share code, notes, and snippets.

@ragowthaman
Created July 7, 2016 09:38
Show Gist options
  • Select an option

  • Save ragowthaman/5035c49d940f104d0f97b52dd72d4b81 to your computer and use it in GitHub Desktop.

Select an option

Save ragowthaman/5035c49d940f104d0f97b52dd72d4b81 to your computer and use it in GitHub Desktop.
How to delete a list element via ajax
def delete_practice(request):
if request.is_ajax and request.POST:
kb_practice_id = request.POST.get('kb_practice_id')
Practice.objects.get(id=kb_practice_id).delete()
return HttpResponse(json.dumps({}), content_type="application/json")
else:
raise Http404
function delete_practice(){
$(".actions .delete-practice").click(function (){
var isGood=confirm('Irreversible Action! Are you sure want to delete this PRACTICE from KB once for all?');
if (isGood) {
var $row = $(this).parents(".practice-row");
var $practice_id = $(this).parents(".practice-row").find(".practice-id input").val();
console.log("$practice_id id")
console.log($practice_id)
console.log($row)
console.log("$practice_id id")
//delete from database as well
$.ajax({
type: "POST",
url: "/knowledgebase/delete/practice/",
dataType: "json",
data: {"kb_practice_id": $practice_id},
success: function (data) {
//console.log("all good");
//remove it from screen if success in deleting at the database
$($row).remove();
},
error: function (data, errmsg, err) {
console.log("something wrong in deleting practice");
console.log(data);
console.log(errmsg);
console.log(err)
}
});
}
})
}
<TD>
<a href="/knowledgebase/delete/practice/{{ practice.id }}/">Delete Practice</a>
</TD>
url(r'^delete/practice/', 'knowledgebase.ajax.delete_practice'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment