How can I count total likes?

Issue

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

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.

Answered By – mentix02

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published