Django blog.models.Post.DoesNotExist: Post matching query does not exist

I am currently trying to add an Ajax to my like button to prevent refreshing, I am not an expert in JS. I am trying to learn from mistakes and small training exercises like this one but I am getting the following error:

blog.models.Post.DoesNotExist: Post matching query does not exist.

and I don’t know the source and reason of this error.

Here is the

def like_post(request):
    user = request.user

    if request.method == 'POST':
        post_id = request.POST.get('post_id')
        post_obj = Post.objects.get(id=post_id)
        if user in post_obj.liked.all():

        like, created = Like.objects.get_or_create(user=user, post_id=post_id)
        if not created:
            if like.value == 'Like':
                like.value = 'Unlike'
                like.value = 'Like'
        context = {
            'post_id': post_id,
        if request.is_ajax:
            html = render_to_string('blog/like_section.html',context, request=request)
            return JsonResponse({'form': html})
    return redirect('blog:post-detail', slug=post_obj.slug)

Here is the template like-section.html

          <form action="{% url 'blog:like-post' %}" method="POST" class="like-form" id="{{}}">
            {% csrf_token %}
            <input type="hidden" name="post_id" value='{{}}'>
            {% if user not in post.liked.all %}
              <button id="like" class="bwhite sm-button" style="color: grey; background-color: Transparent; background-repeat:no-repeat; border: none; cursor:pointer; overflow: hidden; outline:none;">
                <i class="far fa-thumbs-up" type="submit"></i>
            {% else %}
              <button id="like" class="bwhite sm-button" style="color: blue;background-color: Transparent; background-repeat:no-repeat; border: none; cursor:pointer; overflow: hidden; outline:none;" >
                <i class="far fa-thumbs-up" type="submit"></i>
            {% endif %}
            <div class="like-count{{}}">{{ post.num_likes }} Likes</div>

Here is the script

<script src=""></script>
<script type="text/javascript"></script>
        $(document).on('click', '#like', function(event){
          var pk = $(this).attr('value');
            type: 'POST',
            url: '{% url "blog:like-post" %}',
            data: {'post_id':pk, 'csrfmiddlewaretoken': '{{ csrf_token }}'},
            dataType: 'json',
            success: function(response){
            error: function(rs, e){

here is the post-details.html

        <!-- Like -->
        <div id="like-section">
        {% include 'blog/like_section.html' %}
        <!-- Like -->


I guest post_obj = Post.objects.get(id=post_id) cause your code exception ( For advise, there are 2 solutions:

# solution 1
    post_obj = Post.objects.get(id=post_id)
except Post.DoesNotExist:
    # do something

# solution 2
post_obj = Post.objects.filter(id=post_id).first()
if not post_obj:
    # do something

For me, I prefer solution 2 because it is more Pythonic 🙂 by using the EAFP principle (What is the EAFP principle in Python?)