[Fixed]-How to make dislike button display immediately after liking it and vice versa? In Django

1👍

$('#likes').click(function(){
    $('.like_span').toggleClass('glyphicon-thumbs-down glyphicon-thumbs-up');
    var postid;
    postid= $(this).attr("data-post_id");
    $.get('/blog/like_post/', {post_id: postid, liked:$('.like_span').hasClass('glyphicon-thumbs-up')}, function(data){
               $('#like_count').html(data);
               $('#likes').hide();
    });
});

Then add class like_span

<span class="like_span glyphicon glyphicon-thumbs-up"></span>
<span class="like_span glyphicon glyphicon-thumbs-down"></span>

Then in views you will get request.GET['liked'] as true or false.Treat that value for increase and decrease

0👍

One of the good ways to handle is not to refresh the page when user click on like or dislike.

Instead, use jquery ajax with rest api(DRF) to increase and decrease the like count.

Use jquery to switch the icons instead of django template tag condition {% if liked %}

Leave a comment