“要解包的值太多”异常

2024-04-26 15:12:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在Django做一个项目,我刚刚开始尝试扩展用户模型,以便创建用户配置文件。

不幸的是,我遇到了一个问题:每次尝试在模板中获取用户的配置文件时(例如,user.get_template.lastIP),都会出现以下错误:

Environment:

Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.1
Python Version: 2.6.1

Template error:
In template /path/to/base.tpl, error at line 19
   Caught an exception while rendering: too many values to unpack

19 :                Hello, {{user.username}} ({{ user.get_profile.rep}}). How's it goin? Logout


Exception Type: TemplateSyntaxError at /
Exception Value: Caught an exception while rendering: too many values to unpack

你知道我做错了什么吗?


Tags: todjango用户angetversionrequest配置文件
3条回答

这个问题看起来很常见,所以我想看看是否可以从有限的信息量中复制。

快速搜索在James Bennett的博客here中找到了一个条目,其中提到在使用UserProfile扩展用户模型时,settings.py中的一个常见错误可能会导致Django抛出此错误。

引用日志:

The value of the setting is not "appname.models.modelname", it's just "appname.modelname". The reason is that Django is not using this to do a direct import; instead, it's using an internal model-loading function which only wants the name of the app and the name of the model. Trying to do things like "appname.models.modelname" or "projectname.appname.models.modelname" in the AUTH_PROFILE_MODULE setting will cause Django to blow up with the dreaded "too many values to unpack" error, so make sure you've put "appname.modelname", and nothing else, in the value of AUTH_PROFILE_MODULE.

如果OP复制了更多的回溯,我希望看到类似下面的内容,我可以通过在AUTH_PROFILE_MODULE设置中添加“models”来复制。

TemplateSyntaxError at /

Caught an exception while rendering: too many values to unpack

Original Traceback (most recent call last):
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/debug.py", line 71, in render_node
    result = node.render(context)
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/debug.py", line 87, in render
    output = force_unicode(self.filter_expression.resolve(context))
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/__init__.py", line 535, in resolve
    obj = self.var.resolve(context)
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/__init__.py", line 676, in resolve
    value = self._resolve_lookup(context)
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/__init__.py", line 711, in _resolve_lookup
    current = current()
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/contrib/auth/models.py", line 291, in get_profile
    app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
ValueError: too many values to unpack

我认为这是Django仍然有一些导入魔力的少数情况之一,当一个小错误没有抛出预期的异常时,这种魔力往往会导致混淆。

你可以在回溯的末尾看到,我发布的AUTH_PROFILE_模块使用“app name.modelname”表单以外的任何其他表单都会导致“app_label,model_name=settings.AUTH_PROFILE_MODULE.split('.')”行抛出“要解包的值太多”错误。

我99%肯定这是这里遇到的原始问题。

尝试在一个变量中解包

python将把它作为一个列表处理

然后从列表中解包

def returnATupleWithThreeValues():
    return (1,2,3)
a = returnATupleWithThreeValues() # a is a list (1,2,3)
print a[0] # list[0] = 1
print a[1] # list[1] = 2
print a[2] # list[2] = 3

该异常意味着您正试图解包元组,但元组对于目标变量的数量有太多的值。例如:这个工作,然后打印1,然后打印2,然后打印3

def returnATupleWithThreeValues():
    return (1,2,3)
a,b,c = returnATupleWithThreeValues()
print a
print b
print c

但这会引起你的错误

def returnATupleWithThreeValues():
    return (1,2,3)
a,b = returnATupleWithThreeValues()
print a
print b

提高

Traceback (most recent call last):
  File "c.py", line 3, in ?
    a,b = returnATupleWithThreeValues()
ValueError: too many values to unpack

现在,我不知道为什么你会这样,但也许这个答案会给你指明正确的方向。

相关问题 更多 >