“要解压的值太多"例外

2022-01-31 00:00:00 python django

问题描述

我正在使用 Django 开发一个项目,并且我刚刚开始尝试扩展 User 模型以制作用户配置文件.

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

<上一页>环境:请求方法:GET请求网址:http://localhost:8000/Django 版本:1.1Python版本:2.6.1模板错误:在模板/path/to/base.tpl 中,第 19 行出现错误渲染时遇到异常:解包的值太多19:你好,{{user.username}} ({{ user.get_profile.rep}}).近况如何?登出异常类型:TemplateSyntaxError at/异常值:渲染时捕获异常:解包的值太多

关于发生了什么或我做错了什么有什么想法吗?

解决方案

该异常表示您正在尝试解包一个元组,但元组的值相对于目标变量的数量过多.例如:这项工作,并打印 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.

相关文章