quot;Too many values to unpackquot; Exception(“要解压的值太多例外)
问题描述
我正在使用 Django 开发一个项目,并且我刚刚开始尝试扩展 User 模型以制作用户配置文件.
不幸的是,我遇到了一个问题:每次我尝试在模板(例如,user.get_template.lastIP
)中获取用户的个人资料时,都会收到以下错误:
关于发生了什么或我做错了什么有什么想法吗?
该异常表示您正在尝试解包一个元组,但元组的值相对于目标变量的数量过多.例如:这项工作,并打印 1,然后 2,然后 3
def returnATupleWithThreeValues():返回 (1,2,3)a,b,c = returnATupleWithThreeValues()打印一个打印 b打印 c
但这会引发你的错误
def returnATupleWithThreeValues():返回 (1,2,3)a,b = returnATupleWithThreeValues()打印一个打印 b
加注
Traceback(最近一次调用最后一次):文件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.
这篇关于“要解压的值太多"例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“要解压的值太多"例外
基础教程推荐
- 将 YAML 文件转换为 python dict 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01