小编典典

“Too many values to unpack” Exception

django

我正在Django中进行项目开发,我刚刚开始尝试扩展User模型以创建用户个人资料。

不幸的是,我遇到了一个问题:每次尝试在模板(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

关于发生了什么或我做错了什么的任何想法?


阅读 442

收藏
2020-03-26

共1个答案

小编典典

该异常意味着你要解压缩一个元组,但是相对于目标变量的数量,该元组的值太多。例如:这项工作,先打印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

现在,我不知道在你的情况下发生这种情况的原因,但也许此答案将为你指明正确的方向。

2020-03-26