how do we handle and create like button in django?

Submitted 3 years, 8 months ago
Ticket #60
Views 1360
Language/Framework Django
Priority Low
Status Closed

Hello,

I have a django web app and i want to add some like button in my feeds like facebook,twitter etc. But I have no idea how to create and handle likes in django . Can anyone help or guide me ? Waiting for your reply.

Thank you

Submitted on Aug 01, 20
add a comment

1 Answer

Verified

@Prameessa249 . 

You have to come up with below steps in order to achive that,

  • Model design
  • Business logics in views
  • Some logics in templates

Let say you have comment model and for each comment you could design LIKES/DISLIKES accordingly,

class Comment(models.Model):
    user =  models.ForeignKey(User, related_name='comments', on_delete=models.CASCADE)
    comment = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    def get_total_likes(self):
        return self.likes.users.count()
class Like(models.Model):
    comment = models.OneToOneField(Comment, related_name="likes", on_delete=models.CASCADE)
    users = models.ManyToManyField(User, related_name='comment_likes')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Based on this high level template, you can move forward with the view & template logic.

Submitted 3 years, 6 months ago

As we havent get any update on this. Hopefully this issue has been resolved. So we are closing the ticket from our end.

- Vengat 3 years, 8 months ago


Latest Blogs