2

I have an error : too many values to unpack It seems to be a problem with my list or may be it's an issue about the way I fill it.

views.py

        favs = FavoriteGames.objects.filter(user_id=request.user.id).values_list('game_id', flat=True)
        mylist = []
        for fav in favs:
            game = Games.objects.get(id=fav)
            mylist.append((game.id, game.guid, game.title, game.logo, "checked"), )
            nogame = Games.objects.filter(~Q(id__in=favs)).values_list('id', 'guid', 'title', 'logo')
        form = GamesEditorForm(games=mylist)

forms.py

class GamesEditorForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self.games = kwargs.pop('games')
        super(GamesEditorForm, self).__init__(*args, **kwargs)
        self.fields['favorite_games'].choices = self.games  

favorite_games = forms.MultipleChoiceField(
    required=True,
    initial=True,
    widget=forms.CheckboxSelectMultiple(),
    )

template

            {% for jeux in form.favorite_games %}
             <p>{{ jeux }}</p>
            {% endfor %}

in forms.py, mylist return :

[(1, 'paragon', 'Paragon', 'paragon.png', 'checked')]

This is my trackback :

Django Version: 1.11.2
Python Version: 3.5.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'widget_tweaks',
 'start',
 'myaccount']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /home/django-project/agora/myaccount/templates/base.html, error at line 16
   too many values to unpack (expected 2)   6 : <meta charset="UTF-8">
   7 : <meta name="viewport" content="width=device-width, initial-scale=1">
   8 : <link rel="stylesheet" href="{% static "myaccount/css/w3/w3.css" %}">
   9 : <link rel="stylesheet" href="{% static "myaccount/css/w3/w3-theme-blue-grey.css" %}">
   10 : <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Open+Sans'>
   11 : <body class="w3-theme-l5">
   12 : 
   13 : <!-- Navbar small -->
   14 : <div class="w3-top">
   15 :  <div class="w3-bar w3-theme-d2 w3-left-align w3-large">
   16 :   <a class="w3-bar-item w3-button w3-hide-medium w3-hide-large w3-right w3-padding-large w3-hover-white w3-large w3-theme-d2" href="javascript:void(0);" onclick="openNav()"><i class="fa fa-bars"></i><img src="https://www.w3schools.com/w3images /avatar2.png" class="w3-circle" style ="height:25px;width:25px" alt="Avatar"><span class="w3-badge w3-right w3-small w3-green">0</span> {{ request.user.username }}</a>
   17 :   <a href="#" class="w3-bar-item w3-button w3-padding-large w3-theme-d4"><i class="fa w3-margin-right"></i>Agora</a>
   18 : <!-- Navbar big -->
   19 : 
   20 : <div class="w3-dropdown-hover w3-hide-small w3-right">
   21 :     <button class="w3-button w3-padding-large"><img src="https://www.w3schools.com/w3images/avatar2.png" class="w3-circle" style="height:25px;width:25px" alt="Avatar"><span class="w3-badge w3-right w3-small w3-green">0</span> {{ request.user.username }}</button>     
   22 :     <div class="w3-dropdown-content w3-card-4 w3-bar-block" style="width:300px">
   23 :       <a href="{% url 'recap' %}" class="w3-bar-item w3-button">Mon compte</a>
   24 :       <a href="{% url 'logoff' %}" class="w3-bar-item w3-button">Déconnexion</a>
   25 :     </div>
   26 :   </div>


Traceback:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/django-project/agora/myaccount/views.py" in view_favorite
  121.  return render(request, 'favorite.html', locals())

File "/usr/local/lib/python3.5/dist-packages/django/shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "/usr/local/lib/python3.5/dist-packages/django/template/loader.py" in render_to_string
  68.     return template.render(context, request)

File "/usr/local/lib/python3.5/dist-packages/django/template/backends/django.py" in render
  66.             return self.template.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  207.                     return self._render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/loader_tags.py" in render
  177.             return compiled_parent._render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  990.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
  957.             return self.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/defaulttags.py" in render
  173.             len_values = len(values)

File "/usr/local/lib/python3.5/dist-packages/django/forms/boundfield.py" in __len__
  71.         return len(self.subwidgets)

File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py" in __get__
  35.         res = instance.__dict__[self.name] = self.func(instance)

File "/usr/local/lib/python3.5/dist-packages/django/forms/boundfield.py" in subwidgets
  57.             for widget in self.field.widget.subwidgets(self.html_name, self.value(), attrs=attrs)

File "/usr/local/lib/python3.5/dist-packages/django/forms/boundfield.py" in <genexpr>
  56.             BoundWidget(self.field.widget, widget, self.form.renderer)

File "/usr/local/lib/python3.5/dist-packages/django/forms/widgets.py" in subwidgets
  559.         for option in self.options(name, value, attrs):

File "/usr/local/lib/python3.5/dist-packages/django/forms/widgets.py" in options
  564.         for group in self.optgroups(name, value, attrs):

File "/usr/local/lib/python3.5/dist-packages/django/forms/widgets.py" in optgroups
  573.         for index, (option_value, option_label) in enumerate(chain(self.choices)):

Exception Type: ValueError at /myaccount/favorite/
Exception Value: too many values to unpack (expected 2)

1 Answers1

4

The problem is that in self.games you have a list with items where each item contains 5 elements, eg. (1, 'paragon', 'Paragon', 'paragon.png', 'checked')

Each choice in forms.MultipleChoiceField is forms.Choicefield which accepts a pair of values. The first one is for value itself (eg. 1 for pk) and the second one is for human-readable name (eg. Paragon for title).

In your case choices which are passed by mylist variable should be equal to something like: [(1, 'Paragon'), (2, 'Paragon2'),]

It also looks like you're trying to mark them as "checked". If you want to specify default values for them you can use initial field, here you can find some examples.

Kamil
  • 1,256
  • 10
  • 17