每个用户唯一的提交btn - django

问题描述 投票:0回答:1

我的帖子有一个“喜欢”按钮,用户可以喜欢或不喜欢这些帖子,这意味着如果用户第一次单击该按钮,该帖子将被用户喜欢,而第二次单击该按钮将被用户不喜欢。用户。
就像 Facebook 或 Twitter 等一样。我尝试了很多方法来做到这一点,但我的问题是“当用户单击按钮时,它会在计数器上添加一个喜欢,如果用户再次喜欢,它会计算该帖子的 2 个喜欢”用户!
不管怎样,我们怎样才能让点赞变得独一无二呢?每个用户应该只能对任何帖子点赞一次,如果他们再次点击,则意味着他们不喜欢某个帖子。

if request.POST.get("single_tweet_like_submit_btn"):
        try:
            # Debug: Check the request and user
            print(f"Request by user: {current_basic_user_profile.user.username} for tweet ID: {current_tweet.id}")

            # Check if the user has already liked the tweet
            tweet_like = TweetLike.objects.filter(tweet=current_tweet, liker=current_basic_user_profile)
            
            if tweet_like:
                # Debug: User has already liked the tweet, so we'll unlike it
                print(f"User {current_basic_user_profile.user.username} has already liked tweet {current_tweet.id}, unliking it.")
                
                # If the user has already liked the tweet, unlike it
                tweet_like.delete()
                
                # Decrement the like count
                current_tweet.tweet_like_amount = max(0, current_tweet.tweet_like_amount - 1)
            else:
                # Debug: User has not liked the tweet yet, so we'll like it
                print(f"User {current_basic_user_profile.user.username} has not liked tweet {current_tweet.id}, liking it.")

                # If the user hasn't liked the tweet, like it
                new_like = TweetLike(tweet=current_tweet, liker=current_basic_user_profile)
                new_like.save()
                
                # Increment the like count
                current_tweet.tweet_like_amount += 1
                
                # Create a notification for the like
                new_notification = NotificationLike(
                    notified=current_tweet.user,
                    notifier=current_basic_user_profile,
                    tweet=current_tweet,
                )
                new_notification.save()
            
            # Save the current tweet's state
            current_tweet.save()

            # Debug: Output the new like count
            print(f"Tweet {current_tweet.id} now has {current_tweet.tweet_like_amount} likes.")
            
        except Exception as e:
            # Handle exceptions appropriately
            print(f"An error occurred: {e}")  # For debugging purposes
        
        return HttpResponseRedirect(f"/tweet/{current_tweet.id}/")

我尝试了很多方法来解决这个问题但没有成功:)

python django django-views django-forms
1个回答
0
投票

你的方法几乎是正确的,确保用户只能喜欢或不喜欢一个帖子一次,你需要检查喜欢是否已经存在,然后删除或添加它,相应地调整喜欢计数。

if request.POST.get("single_tweet_like_submit_btn"):
    try:
        print(f"Request by user: {current_basic_user_profile.user.username} for tweet ID: {current_tweet.id}")

        # Toggle like status
        tweet_like, created = TweetLike.objects.get_or_create(tweet=current_tweet, liker=current_basic_user_profile)
        
        if created:
            print(f"User {current_basic_user_profile.user.username} liked tweet {current_tweet.id}.")
            current_tweet.tweet_like_amount += 1
            NotificationLike.objects.create(
                notified=current_tweet.user,
                notifier=current_basic_user_profile,
                tweet=current_tweet,
            )
        else:
            print(f"User {current_basic_user_profile.user.username} unliked tweet {current_tweet.id}.")
            tweet_like.delete()
            current_tweet.tweet_like_amount = max(0, current_tweet.tweet_like_amount - 1)
        
        current_tweet.save()
        print(f"Tweet {current_tweet.id} now has {current_tweet.tweet_like_amount} likes.")
        
    except Exception as e:
        print(f"An error occurred: {e}")

    return HttpResponseRedirect(f"/tweet/{current_tweet.id}/")

我的代码使用 get_or_create 来简化检查同类是否已存在并相应地更改同类状态的过程。

© www.soinside.com 2019 - 2024. All rights reserved.