Search code examples
pythondjango

"Too many values to unpack" Exception


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?


Solution

  • 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 works, 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.