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 views.py
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(): post_obj.liked.remove(user) else: post_obj.liked.add(user) like, created = Like.objects.get_or_create(user=user, post_id=post_id) if not created: if like.value == 'Like': like.value = 'Unlike' else: like.value = 'Like' like.save() 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="{{post.id}}"> {% csrf_token %} <input type="hidden" name="post_id" value='{{post.id}}'> {% 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> </button> {% 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> </button> {% endif %} <div class="like-count{{post.id}}">{{ post.num_likes }} Likes</div> </form>
Here is the script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script type="text/javascript"></script> <script> $(document).on('click', '#like', function(event){ event.preventDefault(); var pk = $(this).attr('value'); $.ajax({ type: 'POST', url: '{% url "blog:like-post" %}', data: {'post_id':pk, 'csrfmiddlewaretoken': '{{ csrf_token }}'}, dataType: 'json', success: function(response){ $('#like-section').html(response['form']) console.log($('#like-section').html(response['form'])); }, error: function(rs, e){ console.log(rs.responseText); }, }); }); </script>
here is the post-details.html
<!-- Like --> <div id="like-section"> {% include 'blog/like_section.html' %} </div> <!-- Like -->
Advertisement
Answer
I guest post_obj = Post.objects.get(id=post_id)
cause your code exception (https://docs.djangoproject.com/en/3.1/ref/exceptions/#objectdoesnotexist). For advise, there are 2 solutions:
# solution 1 try: 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?)