Django Like按钮 [英] Django Like Button

查看:108
本文介绍了Django Like按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的应用程序在每个板上创建一个像我的宠物图片的按钮,但是我无法弄清楚如何创建一个,因为它包含整数。通常我有一个想法和理解的功能我创建。



当用户点击相似按钮时。类似的按钮将增加1,它将显示在图片附近。



这是我的图片模块。

  class Picture(models.Model):
user = models.ForeignKey(User)
board = models.ForeignKey(Board,related_name ='lo')
image = models.FileField(upload_to =images /,blank = True,null = True)
description = models.TextField()
is_primary = models.BooleanField(default = False)

def __unicode __(self):
return self.description

可以有人请帮我创建一个类似按钮的基础知识?所以我可以理解函数的逻辑。

解决方案

我认为很多用户可以喜欢很多照片。



你需要另一个模型:

  class Like(models.Model):
user = models.ForeignKey(User)
picture = models.ForeignKey(Picture)
created = models.DateTimeField(auto_now_add = True)
pre>

并调用像这样的喜欢的数量:

  p = Picture.objects.get(...)
number_of_likes = p.like_set.all()。count()

为了增加您在某个视图中的喜好数量:

  def like request,picture_id):
new_like,created = Like.objects.get_or_create(user = request.user,picture_id = picture_id)
如果没有创建:
#用户已经喜欢这张照片
else:
#oll korrekt

so w没有人点击两次相同的按钮,他只能算一个。



查看当前用户是否已经喜欢显示的图像:

  def picture_detail(request,id):
pic = get_object_or_404(Picture,pk = id)
user_likes_this = like_set.filter(user = request.user)和True或False

希望这有帮助。 >

I'm been trying to create a like button for my pet pictures in each board for my app but I can't figure out how to create one because it contain Integer .Usually I have an idea and understanding of the functions I create.

When the user clicks on the like button . The like button will increase by 1 and it will display near the picture.

This is my picture module.

class Picture(models.Model):
    user = models.ForeignKey(User)
    board = models.ForeignKey(Board ,related_name='lo')
    image = models.FileField(upload_to="images/",blank=True,null=True)
    description = models.TextField()
    is_primary = models.BooleanField(default=False)

    def __unicode__(self):
        return self.description

Can someone please help me create the basics of a like button? So I can understand the logic of the function.

解决方案

I assume that many users can like many pictures.

You'll need another model:

class Like(models.Model):
    user = models.ForeignKey(User)
    picture = models.ForeignKey(Picture)
    created = models.DateTimeField(auto_now_add=True)

And call the number of likes like this:

p = Picture.objects.get(...)
number_of_likes = p.like_set.all().count()

To increase the number of likes you might so something like that in a view:

def like(request, picture_id):
    new_like, created = Like.objects.get_or_create(user=request.user, picture_id=picture_id)
    if not created:
        # the user already liked this picture before
    else:
        # oll korrekt

so whenever someone clicks on the same like button twice, he only counts as one.

To find out if the current user already likes the displayed image or not:

def picture_detail(request, id):
    pic = get_object_or_404(Picture, pk=id)
    user_likes_this = pic.like_set.filter(user=request.user) and True or False

Hope this helps.

这篇关于Django Like按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆