Skip to content
Advertisement

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:

JavaScript

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

Here is the views.py

JavaScript

Here is the template like-section.html

JavaScript

Here is the script

JavaScript

here is the post-details.html

JavaScript

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:

JavaScript

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

Advertisement