简体   繁体   English

分配前在/本地变量'user'处引用了UnboundLocalError

[英]UnboundLocalError at / local variable 'user' referenced before assignment

I'm using django-favorite, https://bitbucket.org/last_partizan/django-favorites and trying to display "Get only blogpost favorites for user" strategy to get my favorites. 我正在使用django-favorite, https: //bitbucket.org/last_partizan/django-favorites并尝试显示“仅获取用户的博客文章收藏夹”策略以获取我的收藏夹。 in documentation, it says 在文档中,它说

content_type = get_object_or_404(ContentType, app_label='myblogapp', model='blogpost')
favs = Favorite.objects.favorites_for_user(user).filter(content_type=content_type)

will do the job. 会做的工作。 So in my views.py I have added 所以在我的views.py中,我添加了

def index(request):
        user = MyProfile.objects.get(username=user)

        categories = Category.objects.all()
        try:
                sort = request.GET["sort"].strip()
                sort_method = SortMethods[sort]
                page = request.GET["page"].strip()
        except KeyError:
                sort_method = SortMethods.score
                page = 1

        if sort_method == SortMethods.date:
                post_list = Post.objects.order_by("-pub_date")
        else:
                post_list = Post.objects.all()
                post_list = sorted(post_list, key=lambda x: x.get_score(), reverse=True)

        paginator = Paginator(post_list, 30)

        try:
                posts = paginator.page(page)
        except PageNotAnInteger:
                posts = paginator.page(1)
        except EmptyPage:
                posts = paginator.page(paginator.num_pages)

        content_type = get_object_or_404(ContentType, app_label='main', model='post')
        favs = Favorite.objects.favorites_for_user(user).filter(content_type=content_type)
        context = {
                "posts": posts,
                "pages": paginator.page_range,
                "sort": sort_method.name,
                "categories":categories,
                "favs":favs,

        }
        return render(request, "main/index.html", context)

and to use that fav in my index.html, I have simply put {{fav}}, I'm not even sure this is the right way to call the favorites. 并在我的index.html中使用该收藏夹,我只是简单地输入了{{fav}},我什至不知道这是调用收藏夹的正确方法。 But I tried and now it gives me UnboundLocalError at / 但我尝试过,现在它在/下给了我UnboundLocalError

local variable 'user' referenced before assignment that's coming from ` user = MyProfile.objects.get(username=user) 来自`user = MyProfile.objects.get(username = user)的赋值之前引用的局部变量'user'

` I have userena app, inside userena model `我有userena应用程序,在userena模型中

  class MyProfile(UserenaBaseProfile):
    user = models.OneToOneField(User,
                                unique=True,
                                verbose_name=_('user'),
                                related_name='my_profile')
    intro = models.CharField(_('intro'),
                                       max_length=5, default="Hello")

I'm calling from here to get user...I hope this is right. 我从这里打电话来吸引用户...我希望这是正确的。

Should that first line be using: 第一行应该使用:

request.user

So like this: 像这样:

user = MyProfile.objects.get(username=request.user)

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

相关问题 UnboundLocalError:分配前已引用本地变量“用户” - UnboundLocalError: Local variable 'user' referenced before assignment 在 Django 中分配之前引用 /signin/ 局部变量“用户”处的 UnboundLocalError - UnboundLocalError at /signin/ local variable 'user' referenced before assignment in Django UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment unboundLocalError:赋值前引用了局部变量“loopback” - unboundLocalError: local variable 'loopback' referenced before assignment UnboundLocalError:分配前已引用局部变量“ endProgram” - UnboundLocalError: local variable 'endProgram' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM