“要解压缩的太多值”例外 [英] "Too many values to unpack" Exception

查看:169
本文介绍了“要解压缩的太多值”例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django开展一个项目,我刚刚开始尝试扩展用户模型,以便创建用户个人资料。



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

 
环境:

请求方法:GET
请求URL:http:// localhost:8000 /
Django版本:1.1
Python版本:2.6.1

模板错误:
在模板/path/to/base.tpl中,第19行错误
在渲染时捕获异常:太多值要解压缩

19:您好,{{user.username} }({{user.get_profile.rep}})。近况如何?注销


例外类型:TemplateSyntaxError at /
异常值:渲染时捕获异常:太多值要解压缩

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

解决方案

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

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

但是这会引发你的错误

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

raise

 追溯(最近的最近通话):
文件c.py,第3行,在?
a,b = returnATupleWithThreeValues()
ValueError:太多值要解压缩

现在,为什么这种情况发生在你的情况下,我不知道,但也许这个答案会指向你正确的方向。


I'm working on a project in Django and I've just started trying to extend the User model in order to make user profiles.

Unfortunately, I've run into a problem: Every time I try to get the user's profile inside of a template (user.get_template.lastIP, for example), I get the following error:

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

Any ideas as to what's going on or what I'm doing wrong?

解决方案

That exception means that you are trying to unpack a tuple, but the tuple has too many values with respect to the number of target variables. For example: this work, and prints 1, then 2, then 3

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

But this raises your error

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

raises

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

Now, the reason why this happens in your case, I don't know, but maybe this answer will point you in the right direction.

这篇关于“要解压缩的太多值”例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆