在邮件中发送将用户重定向到接受/拒绝页面的超链接后,如何在Django中接受好友请求?

时间:2019-11-19 05:40:07

标签: django

models.py:

class Friend(models.Model, LoginRequiredMixin):
        status = models.CharField(max_length=10)
        from_user = models.ForeignKey(AUTH_USER_MODEL, on_delete=models.CASCADE, related_name = 'from_user')
        to_user = models.ForeignKey(AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="to_user")
        date_modified = models.DateTimeField(auto_now=True, blank=True)
        date_created = models.DateTimeField(auto_now_add=True, null=True)

        def create(self,request, **kwargs):
            friend = self.create(from_user_id=request.user.id, status="pending")
            return friend

views.py: 该视图包含2个按钮:接受和拒绝,并根据用户的响应在数据库中进行更新

 def accept_friend_request(request, uidb64, status):
     """Accept button will lead to entry in database as accepted and reject button will lead to entry in database as rejected  based on status flag"""
    try:
        uid = urlsafe_base64_decode(uidb64).decode()
        friend_user = User.objects.get(id=Friend.to_user.id) & Friend.from_user
        print(friend_user)
        f = Friend.objects.filter(friend_id = friend_user)
        print(f)
        if f:
           f.status = "accepted"
           f.save()
           return render(request, 'users/friend_list.html', {"uidb64": uid, "status": status})
        else:
           f.status = "rejected"
           f.save()
           return render(request, 'users/friend_list.html', {'uidb64':uid, 'status':status})
    except(FieldError, AttributeError):
        return render(request, 'blog/base.html')

点击接受/拒绝不会更新数据库

先谢谢您

1 个答案:

答案 0 :(得分:0)

通过循环Friend.objects.all()解决。

感谢帮助

Akshat