【问题标题】:How to have a follower count on django如何在 django 上获得追随者
【发布时间】:2020-07-22 02:07:59
【问题描述】:

我一直在开发类似 instagram 的关注系统,在该系统中,用户可以关注和取消关注其他用户,个人资料还必须显示所选用户拥有的关注者数量以及该用户关注的人数。一切正常,除了关注者计数没有显示任何数字而不是 0,这是默认值,即使用户有一个或多个关注者。这个django项目怎么添加count函数?

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    profile_pic = models.ImageField(upload_to='profile_pics', null=True, blank=True, default='default.png')
    bio = models.CharField(max_length=400, default=1, null=True)
    connection = models.CharField(max_length = 100, blank=True)
    follower = models.IntegerField(default=0)
    following = models.IntegerField(default=0)

    def __str__(self):
        return f'{self.user.username} Profile'

class Following(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    followed = models.ManyToManyField(User, related_name="followed")
    follower = models.ManyToManyField(User, related_name="follower")

    @classmethod
    def follow(cls, user, another_account):
        obj, create = cls.objects.get_or_create(user = user)
        obj.followed.add(another_account)
        print("followed")

    @classmethod
    def unfollow(cls, user, another_account):
        obj, create = cls.objects.get_or_create(user = user)
        obj.followed.remove(another_account)
        print("unfollowed")

    def __str__(self):
        return f'{self.user.username} Profile'

views.py

def profile(request, username=None):
    profile, created = Profile.objects.get_or_create(user=request.user)
    user = User.objects.filter(username=username)
    if user:
        post_owner = get_object_or_404(User, username=username)
        profile_bio = Profile.objects.filter(user_id=post_owner)
        user_posts = Post.objects.filter(user_id=post_owner)
        user = user[0]
        is_following = Following.objects.filter(user=request.user, followed=user)
        following_obj = Following.objects.get(user=user)
        follower = following_obj.follower.count()
        following = following_obj.followed.count()

    else:
        post_owner = request.user
        user_posts = Post.objects.filter(user=request.user)
        profile_bio = Profile.objetcs.filter(user=request.user)
        
    args1 = {
        'user_obj':user,
        'post_owner': post_owner,
        'user_posts': user_posts,
        'follower': follower,
        'following': following,
        'connection': is_following,
        'profile_bio': profile_bio,
    }
    return render(request, 'profile.html', args1)

def follow(request, username):
    main_user = request.user
    to_follow = User.objects.get(username=username)
    following = Following.objects.filter(user = main_user, followed = to_follow)
    is_following = True if following else False 
    if is_following:
        Following.unfollow(main_user, to_follow)
        is_following = False
    else:
        Following.follow(main_user, to_follow)
        is_following = True
    resp = {
        'following': is_following,
    }
    response = json.dumps(resp)
    return HttpResponse(response, content_type="application/json")

profile.html

<div class="header-item">
  {{ follower }}
</div>
<div class="header-item">
  {{ following }}
</div>

{% if connection %}
  <a type="button" class="button-caballo" id="follow" role="button" href="{% url 'follow' user_obj.username %}">Unfollow</a>
{% elif not connection %}
  <a type="button" class="button-caballo" id="follow" role="button" href="{% url 'follow' user_obj.username %}">Follow</a>
{% endif %}

【问题讨论】:

  • 我不明白你的整数关系模型和双 M2M 关系。这应该是一个单一的 M2M 关系,如果您需要关系上的元数据,可以使用中间模型。
  • @KlausD。你建议添加一个新模型是什么?它应该有什么?

标签: python html django django-models django-views


【解决方案1】:

你必须像这样改变跟随功能:

def follow(request, username):

    main_user = request.user
    to_follow = User.objects.get(username=username)
    following = Following.objects.filter(user = main_user, followed = to_follow).first()
 
    if following is not None:
        Following.unfollow(main_user, to_follow)
        is_following = False
        main_user.following -= 1
        to_follow.follower -= 1
    else:
        Following.follow(main_user, to_follow)
        is_following = True
        main_user.following += 1
        to_follow.follower += 1

    main_user.save()
    to_follow.save()

    resp = {
        'following': is_following,
    }
    response = json.dumps(resp)
    return HttpResponse(response, content_type="application/json")

【讨论】:

  • 嗨,帕思!代码仍然没有显示用户的关注者数量,我现在该怎么办?
  • @JuanMartinZabala 请检查更新的代码 sn-p 它可能对你有用。
  • 我收到此错误File "C:\Users\USER\startup\gstartup\accounts\views.py", line 112, in follow main_user.following -= 1 TypeError: unsupported operand type(s) for -=: 'Following' and 'int' 这些如何解决?
  • 首先你必须得到main_user的对象。检查更新的代码。
猜你喜欢
  • 2011-09-07
  • 2013-09-28
  • 1970-01-01
  • 2020-06-30
  • 2019-07-03
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
相关资源
最近更新 更多