ValueError: Need 2 values to unpack in for loop; got 3 (actually num_loopvars = 2 and len_item = 3)

2,498 views
Skip to first unread message

Ogi Vranesic

unread,
Jul 17, 2017, 9:47:53 AM7/17/17
to Django users

Hi

I have used some templates in older django versions.
But now in django 1.11 using the same templates, the error:

ValueError: Need 2 values to unpack in for loop; got 3.

will be raised on line 207 in module django.template.defaulttags
by the method render of class ForNode

Actually is by me looking at code num_loopvars = 2 and len_item = 3.

After I've outcommented this part of code, some of this template as a form is displayed and one can work on it.

So could somebody tell me what is purpose of this two variables and comparing of them?

Thanks very much in advance and best regards
Ogi


Michal Petrucha

unread,
Jul 17, 2017, 10:09:30 AM7/17/17
to django...@googlegroups.com
On Mon, Jul 17, 2017 at 03:46:30PM +0200, Ogi Vranesic wrote:
> Hi
>
> I have used some templates in older django versions.
> But now in django 1.11 using the same templates, the error:
>
> ValueError: Need 2 values to unpack in for loop; got 3.
>
> will be raised on line 207 in module django.template.defaulttags
> by the method render of class ForNode
>
> Actually is by me looking at code *num_loopvars = 2* and *len_item = 3*.
>
> After I've outcommented this part of code, some of this template as a form
> is displayed and one can work on it.
>
> So could somebody tell me what is purpose of this two variables and
> comparing of them?
>
> Thanks very much in advance and best regards
> Ogi

Sounds like you are trying to loop over a list in your template,
something like this::

{% for a, b in mylist %}
...
{% endfor %}

The problem is that mylist contains triples, not pairs. It would help
a lot if you could show the actual template code, as well as the
context that you pass to the template.

Cheers,

Michal
signature.asc

Ogi Vranesic

unread,
Jul 17, 2017, 10:56:08 AM7/17/17
to Django users

Michal, Thanks very much for your reply.

I guessed, that is something like in your example.
And I find it in template.

So the conclusion is that there is no more allowed in django template engine to use for loop with less elements to unpack,
and I think that is also consistent and more pythonic.

Best regards
Ogi

Reply all
Reply to author
Forward
0 new messages