'How can I count total likes?
I added a like button to my blog. It's working perfectly but I can't count the total number of likes present. What should I do now? How can I count the total number of likes?
models.py:
class FAQ(models.Model):
likes = models.ManyToManyField(User,default=None, related_name="faqLIKES")
views.py:
def index(request):
allFAQ = FAQ.objects.all()
context = {"allFAQ":allFAQ}
return render(request,'0_index.html',context)
Solution 1:[1]
Use count.
>>> post.likes.count() # returns a count of all likes
Also, as a side note - why do you have a url parameter if you're getting the id of the post to like inside the POST data? Why not only use the url parameter? You wouldn't have to send any form data from the client side.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mentix02 |
