I have create a delete function on Django when i try to delete a post using jQuery I am getting this error Field 'id' expected a number but got \n \n \n I also add try to assign id over the pk but I am getting the same error can anybody tell me how can I use this function using jQuery
def ArticleDelete(request):
if request.method == "POST":
pk = request.POST.get('pk')
article = get_object_or_404(Article, pk=pk)
messages.success(request, 'The story was deleted successfully!')
article.delete()
return JsonResponse({"success": True}, status=200)
else:
return JsonResponse({"success": False}, status=400)
urls.py
urlpatterns = [
path('delete/', views.ArticleDelete, name='delete'),
}
index.js
$('.delete').click(function () {
var this_html = $(this);
var pk = this_html.parent().parent().children().first().text();
$.ajax({
url: '{% url "del" %}',
type: 'POST',
headers: { "X-CSRFToken": '{{csrf_token}}' },
data: { pk: pk },
success: function () {
this_html.parent().parent().remove();
}
});
})
var pk, show your template file. also,console.log(pk)may help you debug.