如何让Django中的我的喜欢按钮起作用?

时间:2019-06-24 01:16:57

标签: django django-views

我正在尝试让我的“喜欢”按钮在我的网站上工作。您可以在我的网站上发布“ tweets”,但用户不喜欢该帖子。我已经使用ajax设置了前端。完美的作品。问题出在我的观点上。

这是我的模特

    class Tweet(models.Model):
    tweet_user = models.ForeignKey(User, on_delete=models.CASCADE)
    tweet_message = models.TextField()
    tweet_date = models.DateTimeField(auto_now_add=True)
    tweet_like_counter = models.IntegerField(default=0)
    tweet_picture = models.FileField(null=True,blank=True)


    def __str__(self):
        return self.tweet_message


class Like(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    tweet = models.ForeignKey(Tweet, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.tweet.tweet_message

class Disike(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    tweet = models.ForeignKey(Tweet, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.tweet.tweet_message


class TweetComment(models.Model):
    class Meta:
        ordering = ['-id']

    tweetcomment = models.ForeignKey(Tweet, on_delete=models.CASCADE, related_name='tweetcomments')
    tweetcommentauthor = models.ForeignKey(User,on_delete=models.CASCADE)
    tweetcommentmessage = models.TextField()
    tweetcommentcomment_date_created = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.tweetcommentmessage

这是我的观点

from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
@login_required
def like(request, pk):


    currentTweet = get_object_or_404(Tweet,pk=pk)
    user = User.objects.get(pk=request.user.id)
    like = Like.objects.create(tweet=currentTweet, user=user)
    like_queryset = Like.objects.filter(tweet=currentTweet, user=user)
    dislike_queryset = Disike.objects.filter(tweet=currentTweet, user=user)






    if like_queryset.exists():
        Like.objects.filter(tweet=currentTweet, user=user).delete()
        dislikeobject = Disike.objects.filter(tweet=currentTweet).count()
        likeobject = Like.objects.filter(tweet=currentTweet).count()
        currentTweet.tweet_like_counter = likeobject - dislikeobject
        currentTweet.save()

    if  dislike_queryset.exists():
        Disike.objects.filter(tweet=currentTweet, user=user).delete()
        dislikeobject = Disike.objects.filter(tweet=currentTweet).count()
        likeobject = Like.objects.filter(tweet=currentTweet).count()
        currentTweet.tweet_like_counter = likeobject - dislikeobject
        currentTweet.save()





    return JsonResponse({
        'like_counter': currentTweet.tweet_like_counter


    })




@csrf_exempt
@login_required
def dislike(request, pk):
    currentTweet = get_object_or_404(Tweet, pk=pk)
    user = User.objects.get(pk=request.user.id)
    dislike = Disike.objects.create(tweet=currentTweet, user=user)
    like_queryset = Like.objects.filter(tweet=currentTweet, user=user)
    dislike_queryset = Disike.objects.filter(tweet=currentTweet, user=user)


    if dislike_queryset.exists():
        Disike.objects.filter(tweet=currentTweet, user=user).delete()
        dislikeobject = Disike.objects.filter(tweet=currentTweet).count()
        likeobject = Like.objects.filter(tweet=currentTweet).count()
        currentTweet.tweet_like_counter = likeobject - dislikeobject
        currentTweet.save()

    if like_queryset.exists():
        Like.objects.filter(tweet=currentTweet, user=user).delete()
        dislikeobject = Disike.objects.filter(tweet=currentTweet).count()
        likeobject = Like.objects.filter(tweet=currentTweet).count()
        currentTweet.tweet_like_counter = likeobject - dislikeobject
        currentTweet.save()








    return JsonResponse({
        'like_counter':  currentTweet.tweet_like_counter

    })

我不知道该怎么办。当我运行此代码时,我没有收到任何错误,但是当我单击“赞”按钮时,没有创建类似对象...

但是当我在视图中使用此代码时。

from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
@login_required
def like(request, pk):


    currentTweet = get_object_or_404(Tweet,pk=pk)
    user = User.objects.get(pk=request.user.id)
    like = Like.objects.create(tweet=currentTweet, user=user)
    like_queryset = Like.objects.filter(tweet=currentTweet, user=user)
    dislike_queryset = Disike.objects.filter(tweet=currentTweet, user=user)











    return JsonResponse({
        'like_counter': currentTweet.tweet_like_counter


    })




@csrf_exempt
@login_required
def dislike(request, pk):
    currentTweet = get_object_or_404(Tweet, pk=pk)
    user = User.objects.get(pk=request.user.id)
    dislike = Disike.objects.create(tweet=currentTweet, user=user)
    like_queryset = Like.objects.filter(tweet=currentTweet, user=user)
    dislike_queryset = Disike.objects.filter(tweet=currentTweet, user=user)










    return JsonResponse({
        'like_counter':  currentTweet.tweet_like_counter

    })

创建了一个赞,但是用户将可以继续喜欢相同的帖子。我在这个问题上停留了大约一两天。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我认为您应该先检查是否存在相似对象,然后再创建新的相似对象。像这样:

@csrf_exempt
@login_required
def like(request, pk):
    currentTweet = get_object_or_404(Tweet,pk=pk)
    user = User.objects.get(pk=request.user.id)
    like_queryset = Like.objects.filter(tweet=currentTweet, user=user)

    if not like_queryset.exists():
        like = Like.objects.create(tweet=currentTweet, user=user)
        dislikeobject = Disike.objects.filter(tweet=currentTweet).count()
        likeobject = Like.objects.filter(tweet=currentTweet).count()
        currentTweet.tweet_like_counter = likeobject - dislikeobject
        currentTweet.save()

    return JsonResponse({
        'like_counter': currentTweet.tweet_like_counter
    })

小优化

答案的一个小修改是,您不需要保存类似计数器的DB。您可以在Tweet模型中创建Property方法来获取类似计数器。例如:

class Tweet(...):
     ...  # rest of the code

    @property
    def tweet_like_counter(self):
       likes = self.like_set.all().count()
       dislikes = self.like_set.all().count()
       return likes - dislikes

通过这种方式,您无需在数据库中存储Like计数器,并且like计数将动态计算。使用该属性方法,您可以像这样更新视图:

@csrf_exempt
@login_required
def like(request, pk):
    currentTweet = get_object_or_404(Tweet,pk=pk)
    user = User.objects.get(pk=request.user.id)
    like_queryset = Like.objects.filter(tweet=currentTweet, user=user)

    if not like_queryset.exists():
        like = Like.objects.create(tweet=currentTweet, user=user)

    return JsonResponse({
        'like_counter': currentTweet.tweet_like_counter
    })