“要解压的值太多"例外

2024-04-21Python开发问题
6

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

问题描述

我正在使用 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.

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

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10