22👍
Edit: Updated in light of kibibu’s correction.
I have encountered what I believe is this same error, producing the message:
Caught ValueError while rendering: too many values to unpack
My form class was as follows:
class CalcForm(forms.Form):
item = forms.ChoiceField(choices=(('17815', '17816')))
Note that my choices
type here a tuple. Django official documentation reads as follows for the choices
arg:
An iterable (e.g., a list or tuple) of 2-tuples to use as choices for
this field. This argument accepts the same formats as the choices
argument to a model field.
src: https://docs.djangoproject.com/en/1.3/ref/forms/fields/#django.forms.ChoiceField.choices
This problem was solved by my observing the documentation and using a list of tuples:
class CalcForm(forms.Form):
item = forms.ChoiceField(choices=[('17815', '17816')])
Do note that while the docs state any iterable of the correct form can be used, a tuple of 2-tuples did not work:
item = forms.ChoiceField(choices=(('17815', '17816'), ('123', '456')))
This produced the same error as before.
Lesson: bugs happen.
- [Django]-Django or Ruby-On-Rails?
- [Django]-Celery – Tasks that need to run in priority
- [Django]-Django 1.10.1 'my_templatetag' is not a registered tag library. Must be one of:
1👍
If I had to guess, it’s because whatever is in the administrative template expects a list of tuples, but you’ve instead supplied a tuple of tuples (hence the “too many values”). Try replacing with a list instead:
CATEGORY_CHOICES = [ # Note square brackets.
(1, u'Appetizer'),
(2, u'Bread'),
(3, u'Dessert'),
(4, u'Drinks'),
(5, u'Main Course'),
(6, u'Salad'),
(7, u'Side Dish'),
(8, u'Soup'),
(9, u'Sauce/Marinade'),
(10, u'Other'),
]
- [Django]-Django admin: how to make a readonly url field clickable in change_form.html?
- [Django]-Django Facebook Connect App Recommendation
- [Django]-Automatic creation date for Django model form objects
1👍
Per http://code.djangoproject.com/ticket/972 , you need to move the assignment CATEGORY_CHOICES = ...
outside the class
statement.
- [Django]-Currently using Django "Evolution", is "South" better and worth switching?
- [Django]-Django change default runserver port
- [Django]-Steps to Troubleshoot "django.db.utils.ProgrammingError: permission denied for relation django_migrations"
0👍
I got it working. Most of the ‘too many values to unpack’ errors that i came across while googling were Value Error types. My error was a Template Syntax type. To load my recipe table i had imported a csv file. I was thinking maybe there was a problem somewhere in the data that sqlite allowed on import. So i deleted all data and then added 2 recipes manually via django admin form. The list of recipes loaded after that.
thanks.
- [Django]-Django: Difference between using server through manage.py and other servers like gunicorn etc. Which is better?
- [Django]-Difference between filter with multiple arguments and chain filter in django
- [Django]-How to know current name of the database in Django?
0👍
I just had the same problem… my cvs file came from ms excel and the date fields gotten the wrong format at saving time. I change the format to something like ‘2010-05-04 13:05:46.790454’ (excel gave me 5/5/2010 10:05:47) and voilaaaa no more ‘too many values to unpack’
- [Django]-Easy-to-use django captcha or registration app with captcha?
- [Django]-Deploying Django on Google App Engine ==> ERROR: (gcloud.app.deploy) NOT_FOUND: Unable to retrieve P4SA(…)
- [Django]-Multiple ModelAdmins/views for same model in Django admin
0👍
kibibu’s comment to Kreychek’s answer is correct. This isn’t a Django issue but rather an interesting aspect of Python. To summarize:
In Python, round parentheses are used for both order of operations and tuples. So:
foo = (2+2)
will result in foo being 4, not a tuple who’s first and only element is 4: 4
foo = (2+2, 3+3)
will result in foo being a two-dimensional tuple: (4,6)
To tell Python that you want to create a one-dimensional tuple instead of just denoting the order of operations, use a trailing comma:
foo = (2+2,)
will result in foo being a one-dimensional tuple who’s first and only element is 4: (4,)
So for your scenario:
class CalcForm(forms.Form):
item = forms.ChoiceField(choices=(('17815', '17816'),))
would give what you want. Using a list is a great solution too (more Pythonic in my opinion), but hopefully this answer is informative since this can come up in other cases.
For example:
print("foo: %s" % (foo))
may give an error if foo is an iterable, but:
print("foo: %s" % (foo,))
or:
print("foo: %s" % [foo])
will properly convert foo to a string whether it’s an iterable or not.
Documentation: http://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences
- [Django]-Django: Detect database backend
- [Django]-Django error – matching query does not exist
- [Django]-How to check if ManyToMany field is not empty?
0👍
This error IMO occurs when a function returns three values, and you assign it to 2. e.g.:
def test():
a=0
b=0
c=0
....
a,b=test() <---three values returned, but only assigning to 2.
a,b,c=test() <—three values returned, assigned to 3.OK
- [Django]-What's the proper way to test token-based auth using APIRequestFactory?
- [Django]-Setting DEBUG = False causes 500 Error
- [Django]-Django Templating: how to access properties of the first item in a list