Reputation: 912
I have tried another method to create a like button for posts, I have reached to the phase where button directs me to an error of This page isn’t working
Not sure where the problem relies on. I am following a tutorial but the only difference is that the tutorial created a function for the post with context in the views while I created a class for the post. I don't know if this is the reason for some deviations and the error appearing
Here is the Model:
class Post(models.Model):
designer = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
---- Others unrelated models------
likes = models.ManyToManyField(User, blank=True, related_name='liked')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("score:post-detail", kwargs={'pk': self.pk})
@property
def num_likes(self):
return self.liked.all().count()
LIKE_CHOICES = (
('Like', 'Like'),
('Unlike', 'Unlike'),
)
class Like(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
value = models.CharField(choices=LIKE_CHOICES,
default='Like', max_length=10)
def __str__(self):
return str(self.post)
here is the views
class PostDetailView(DetailView):
model = Post
template_name = "post_detail.html"
class PostListView(ListView):
model = Post
template_name = "score.html"
ordering = ['-date_posted']
context_object_name = 'posts'
queryset = Post.objects.filter(admin_approved=True)
paginate_by = 5
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()
return redirect('score:like_post')
here is the full template
{% extends "base.html"%}
{% block content %}
{% for post in posts %}
<div style="padding-top:200 px"><strong>{{post.title}}</strong></div>
<form action="{% url 'score:like_post'%}" method='POST' class="ui form">
{% csrf_token %}
<input type='hidden' name='post_id' value="{{post.id}}">
{% if user not in post.liked.all %}
<button class= "ui button positive" type='submit'> Like </button>
{% else %}
<button class= "ui button negative" type='submit'> Unlike </button>
{% endif %}
</form>
<strong>{{post.liked.all.count}} Likes </strong>
{% endfor %}
{% endblock content %}
and here is the URL
path('', PostListView.as_view(), name='score'),
path('like/', like_post, name='like_post'),
here is the base
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>
{% block extra_head %}
{% endblock %}
</head>
<body>
<!--Main layout-->
{% block content %}
{% endblock %}
<!--Main layout-->
<!--Footer-->
{% include "footer.html" %}
<!--/.Footer-->
<!-- SCRIPTS -->
{% include "scripts.html" %}
<!-- SCRIPTS -->
{% block extra_scripts %}
{% endblock extra_scripts %}
</body>
</html>
Upvotes: 3
Views: 1322
Reputation: 55
def like_post(request):
user = request.user
post = get_object_or_404(Post,id=request.POST.get('post_id'))
if request.method == 'POST':
post_id = request.POST.get('post_id')
post_obj = Post.objects.get(id=post_id)
if user in post_obj.likes.all():
post_obj.likes.remove(user)
else:
post_obj.likes.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()
return HttpResponseRedirect(post.get_absolute_url())
Instead of redirect you can use HttpResponseRedirect and don't forget to import it from django.http
Upvotes: 0
Reputation: 7744
It seems like a result of infinite recursion, i.e. when you click on the like button it redirects you to views.like_post
, which again redirects to itself. Therefore, causing an infinite redirection issue. You can fix this by redirecting to some other view or go with an initial implementation shown in your tutorial.
Since any view in Django must return an HTTPRESPONSE
, you can just return this
return HttpResponse("OK")
Upvotes: 1