I have implemented a like button onto my view_post page, but the like's aren't been registered. When the button is clicked the page is redirected correctly but no likes are added.
views
def get_post(request, slug):try:post = BlogPost.objects.get(slug=slug)except BlogPost.DoesNotExist: messages.error(request, 'This post does not exist.')post = Nonecomment_form = CommentForm()return render(request, 'mhpapp/view-post.html', {'post': post, 'comment_form': comment_form,})def like_post(request, slug):template_name = 'view-post.html'post = get_object_or_404(BlogPost, slug=slug)liked = Falseif post.likes.filter(id=request.user.id).exists():post.likes.remove(request.user)liked = Falseelse:post.likes.add(request.user)messages.success(request, ("Thanks for the like...:-)"))liked = Truereturn redirect('get_post', {'slug': slug,})
urls
path('<slug:slug>/', views.get_post, name='viewpost'),
path('<slug:slug>/',views.like_post, name='likepost'),
html
<strong>{{ post.total_likes }} Likes</strong>{% if user.is_authenticated %}<form action="{% url 'likepost' post.slug %}" method="POST">{% csrf_token %}{% if request.user in post.likes.all %} <button class="btn btn-outline-secondary rounded-0 custom-button" id="like" type="sumbit" name="post-id" value="{{ post.slug }}"><i class="fa-solid fa-heart-crack"></i></button>{% else %}<button class="btn btn-outline-secondary rounded-0 custom-button" id="like" type="sumbit" name="post-id" value="{{ post.slug }}"><i class="fa-solid fa-heart"></i></button>{% endif %}</form>{% else %}{% endif %}