【问题标题】:UnboundLocalError at / local variable 'Profile' referenced before assignmentUnboundLocalError at / local variable 'Profile' 在赋值之前引用
【发布时间】:2018-12-27 15:25:23
【问题描述】:

我正在写一个如图所示的django视图:-

def feed(request):
if request.user.is_authenticated:
    user=request.user
    profile=Profile.objects.filter(user=user)
    userfollowing=FollowingProfiles.objects.filter(Profile=profile)
    for following in userfollowing:
        username=following.ProfileName
        useraccount=User.objects.filter(username=username)
        Profile=Profile.objects.filter(user=useraccount)
        Post=post.objects.filter(Profile=Profile)
        comment=comment.objects.filter(post=Post)
        final_post_queryset=final_post_queryset+Post
        final_comment_queryset=final_comment_queryset+comment
    return render(request,'feed/feed.html',{'final_comment_queryset':final_comment_queryset,'final_post_queryset':final_post_queryset})
else:
    redirect('signup')

而模板 feed.html 是:-

{% extends 'base.html' %}
{% block content %}
{% load static %}

{% for p in final_post_queryset %}
   {{ p.DatePosted }}
   <img src="{{ p.Picture.url }}"/>
{% endblock %}

虽然错误是:-

所以错误在第三行

profile=Profile.objects.filter(user=user)

【问题讨论】:

    标签: python django django-models django-views


    【解决方案1】:

    我假设您已经导入了 Profile 模块(使用 from .models import Profile 等),并且对它为什么不存在感到困惑(如果您没有,则需要将该导入添加到文件的顶部)。

    即使您已导入它,此代码也不起作用。您的问题是您在函数范围内分配了名称Profile,使其成为局部变量,并且局部变量从函数的开始开始是局部的(但最初为空)。在分配给它们之前尝试访问它们会引发您看到的UnboundLocalError(当您尝试在分配给它之前尝试从本地名称读取时,该错误引发;它不能简单地通过未能导入模块来引发,这只会引发NameError)。即使您确实导入了全局导入的Profile,也看不到它,因为在函数中,Profile 必须是本地的或全局的,不能同时是两者。

    要解决此问题,请为您的局部变量选择一个不同的名称,以便全局 Profile 仍然可以访问:

    def feed(request):
        if request.user.is_authenticated:
            user=request.user
            profile=Profile.objects.filter(user=user)  # This line is fine!
            userfollowing=FollowingProfiles.objects.filter(Profile=profile)
            for following in userfollowing:
                username=following.ProfileName
                useraccount=User.objects.filter(username=username)
                # Use lowercase profile, not Profile
                # Profile=Profile.objects.filter(user=useraccount)  # This line was the problem!
                profile = Profile.objects.filter(user=useraccount)  # This line is the solution!
                Post = post.objects.filter(Profile=profile)  # Change to match new name
                ... rest of your code ...
    

    你之前已经正确使用了小写的profile,而且似乎再也不需要它了,所以我只是重用了这个名字。

    【讨论】:

    • 我也用小写字母
    • @Gagan:你看错了行。代码中的稍后行执行Profile=Profile.objects.filter(user=useraccount)那是行使Profile成为整个feed函数的局部变量,切断访问到前一行的Profileprofile=Profile.objects.filter(user=user)。错误发生在第 40 行,但错误的原因在第 45 行。我编辑了答案以添加回上下文,以便您可以看到错误行在哪里。
    • 非常感谢您的帮助
    • @Gagan:如果它完全回答了您的问题,请投票和/或接受答案(这是确认问题已解决的常用方式)。 IIRC,当您接受答案(这不是您自己的答案)时,您甚至可以自己赚取一点代表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多