Ajax request in django

I implemented the post like feature using ajax in the Django project, but it doesn't work. And it gives a 404 error in the console. template.html

<script>
    $(document).ready(function(){
        $(".line-button").click(function(){
            var post_id = $(this).closest("post").data("post-id");
            var button = $(button);
        $.ajax({
            type: 'POST',
            url: 'like-post/',
            data: {'post_id': post_id, 'csrfmiddlewaretoken':'{{csrf_token}}'},
            
            success: function(data){
                if(data.liked){
                    button.text('unLike');
                }else{
                    button.text('Like');
                }
                $(".likes-count").text(data.post_likes_count);
            }
        })
            })
        })
</script>

views.py

@require_POST
def like_post(request):
    post_id = request.POST.get("post_id")
    post = get_object_or_404(Post, id=post_id)
    if request.user in post.likes.all():
        post.likes.remove(request.user)
        liked = False
    else:
        post.likes.add(request.user)
        liked = True
    post_likes_count = post.likes.count()

    response_data = {
        'liked': liked, 'post_likes_count': post_likes_count
    }
    return JsonResponse(response_data)

urls.py

path('like-post/', views.like_post, name="like_post"),
Вернуться на верх