简体   繁体   English

分配问题之前引用的本地变量“用户”

[英]local variable 'user' referenced before assignment issue

so i currently have my likes app which deals with friend requests, and it works fine however my notification dont seem to be working. 所以我目前有一个喜欢的应用程序,它可以处理朋友的请求,并且工作正常,但是我的通知似乎不起作用。 Whenever some likes someone else regardless of weather they are liked by that user or not it only sends the second of the two notify.send. 每当有人喜欢其他人而不受天气影响时,无论该用户是否喜欢他们,它都只会发送两个notify.send中的第二个。 I presume its an issue with the line "user = get_object_or_404(User, username=user.username)", however i dont know how to get round it. 我认为它与“用户= get_object_or_404(User,username = user.username)”行有关,但是我不知道如何解决。 Here is my code: 这是我的代码:

def like_user(request, id):
    pending_like = get_object_or_404(User, id=id)
    user_like, created = UserLike.objects.get_or_create(user=request.user)
    user = get_object_or_404(User, username=user.username)
    liked_user, like_user_created = UserLike.objects.get_or_create(user=user)
    if pending_like in user_like.liked_users.all():
        user_like.liked_users.remove(pending_like)
    elif request.user in liked_user.liked_users.all():
        user_like.liked_users.add(pending_like)
        notify.send(request.user, 
                        #action=request.user.profile, 
                    target=request.user.profile, 
                    recipient=pending_like,
                    verb='sent you a friend request view'), 
    else:
        user_like.liked_users.add(pending_like)
        notify.send(request.user, 
                        #action=request.user.profile, 
                    target=request.user.profile, 
                    recipient=pending_like,
                    verb='accepted your friend request view')
    return redirect("profile", username=pending_like.username)

Here is an example of where the line " if request.user in liked_user.liked_users.all():" works fine i presume because the line " user = get_object_or_404(User, username=username)" has username in it. 这是一个示例,其中我假定“如果在liked_user.liked_users.all()中的request.user:”行工作正常,因为“ user = get_object_or_404(User,username = username)”行中包含用户名。

@login_required
def profile_view(request, username):
    user = get_object_or_404(User, username=username)
    liked_user, like_user_created = UserLike.objects.get_or_create(user=user)
    do_they_like = False
    if request.user in liked_user.liked_users.all():
        do_they_like = True 

However in my first bit of code I'm trying to use user.username instead of username=username but i get the error "local variable 'user' referenced before assignment". 但是,在我的第一部分代码中,我尝试使用user.username代替username = username,但是出现错误“分配前引用了本地变量'user'”。 What is the best way round this? 最好的办法是什么? am i do it completely wrong? 我做错了吗? should i try and pass in username, because when i do i get the error "like_user() takes exactly 3 arguments (2 given)". 我应该尝试传递用户名吗,因为执行此操作时会出现错误“ like_user()恰好接受3个参数(给定2个)”。 Sorry quite new to django, any help would be massively appreciated! 抱歉,django刚起步,非常感谢您的帮助!

Here is my likes app model incase it helps: 这是我喜欢的应用程序模型,以帮助您:

class UserLike(models.Model):
    user = models.OneToOneField(User, related_name='liker')
    liked_users = models.ManyToManyField(User, related_name='liked_users', blank=True)

    objects = UserLikeManager()


    def __unicode__(self):
        return self.user.username

    def get_mutual_like(self, user_b):
        i_like = False
        you_like = False
        if user_b in self.liked_users.all():
            i_like = True
        liked_user, created = UserLike.objects.get_or_create(user=user_b)
        if self.user in liked_user.liked_users.all():
            you_like = True 
        if you_like and i_like:
            return True
        else:
            return False

really sorry for the long post, but im very stuck as most of this was written by some more advanced than me and im left with the issue of fixing it, any help would be massively appreciated! 真的很抱歉,但是我很固执,因为大部分内容是由比我更先进的一些人写的,而即时消息留下了解决问题的方法,我们将不胜感激!

thanks 谢谢

You are correct, the issue is with the line: 您是对的,问题出在以下方面:

user = get_object_or_404(User, username=user.username)

You're trying to get the attribute 'username' of the 'user' object, but 'user' is not defined within the scope of your function yet. 您正在尝试获取“用户”对象的属性“用户名”,但尚未在功能范围内定义“用户”。 In other words, at that point 'user' doesn't exist yet. 换句话说,那时“用户”还不存在。

I don't have nearly enough info to start helping you, but if it was me debugging that, I'd consider the following: 我没有足够的信息来开始为您提供帮助,但是如果是我进行调试,则需要考虑以下事项:

  • You might not need that problematic line at all. 您可能根本不需要那条有问题的线。 It seems like request.user holds the reference to a user (The one who's liking stuff??), and pending_like seems to contain a reference to the recipient of the like. 似乎request.user拥有对用户(喜欢这个东西的人??)的引用,pending_like似乎包含对like的接收者的引用。 That's probably enough users to establish a 'like' relationship 那可能足以建立“喜欢”关系的用户

  • Maybe do a print of the attributes of pending_like print(pending_like.__dict__) and request.user print(request.user.__dict__) to see if they contain all the info you need? 也许要打印出pending_like print(pending_like.__dict__)和request.user print(request.user.__dict__)的属性,以查看它们是否包含您需要的所有信息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM