我有一个错误:解压缩的值太多了 这似乎是我的列表中的问题,或者可能是我填写它的方式的问题。
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(),
)
模板
{% for jeux in form.favorite_games %}
<p>{{ jeux }}</p>
{% endfor %}
在forms.py,mylist return:
[(1, 'paragon', 'Paragon', 'paragon.png', 'checked')]
这是我的引用:
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)
答案 0 :(得分:2)
问题是在self.games
中你有一个列表,其中每个项目包含5个元素,例如。 (1, 'paragon', 'Paragon', 'paragon.png', 'checked')
forms.MultipleChoiceField中的每个选项都是forms.Choicefield,它接受一对值。第一个是价值本身(例如,1表示pk),第二个是人类可读的名称(例如Paragon作为标题)。
在choices
变量传递的mylist
中,[(1, 'Paragon'), (2, 'Paragon2'),]
应该等于:initial
看起来你也试图将它们标记为“已检查”。如果您想为它们指定默认值,可以使用{{1}}字段,here您可以找到一些示例。