我使用的是WizardView,其中第二个Form是:
FRAME_AND_ALIGNMENT_CHOICES = ["Align Derailleur Hanger", "Basic Clean",
"Install Front Basket"]
class RepairsForm(Form):
frame_and_alignment = forms.MultipleChoiceField(
choices=FRAME_AND_ALIGNMENT_CHOICES,
widget=forms.CheckboxSelectMultiple
)
我有一个像这样的WizardView:
NEW_ORDER_TEMPLATES = {'0': 'app/create_customer.html', '1': 'app/repairs.html'}
class CustomerWizard(SessionWizardView):
def get_template_names(self):
return [NEW_ORDER_TEMPLATES[self.steps.current]]
def done(self, form_list, **kwargs):
customer_id = process_customer(form_list[0])
process_repairs(form_list[1], customer_id)
return HttpResponseRedirect(reverse('app:index'))
最后,这是第二种形式的模板:
<form action="" method="post">
{% csrf_token %}
{{ wizard.management_form }}
{{ wizard.form.frame_and_alignment }}
{# {{ wizard.form.as_p }}#}
<input type="submit" value="Save" />
</form>
表单的第一页填写正常,但当我点击提交以继续此模板和表单时,我在语句{{ wizard.form.frame_and_alignment }}
上得到一个ValueError,其中包含“要解压的值太多(预期2) ”。我的猜测是它与MultipleChoiceField有关。
编辑这是追溯
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/create_order/
Django Version: 1.7.2
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.formtools',
'app')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Template error:
In template /Users/johnking/workspace/rice-bikes/app/templates/app/repairs.html, error at line 10
too many values to unpack (expected 2)
1 : {% extends "app/base.html" %}
2 :
3 : {% block content %}
4 :
5 : {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
6 :
7 : <form action="" method="post">
8 : {% csrf_token %}
9 : {{ wizard.management_form }}
10 : {{ wizard.form.frame_and_alignment }}
11 : {# {{ wizard.form.as_p }}#}
12 : <input type="submit" value="Save" />
13 : </form>
14 :
15 :
16 : {% endblock %}
Traceback:
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
137. response = response.render()
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/response.py" in render
103. self.content = self.rendered_content
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/response.py" in rendered_content
80. content = template.render(context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/base.py" in render
148. return self._render(context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/base.py" in _render
142. return self.nodelist.render(context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/base.py" in render
844. bit = self.render_node(node, context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/debug.py" in render_node
80. return node.render(context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/loader_tags.py" in render
126. return compiled_parent._render(context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/base.py" in _render
142. return self.nodelist.render(context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/base.py" in render
844. bit = self.render_node(node, context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/debug.py" in render_node
80. return node.render(context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/loader_tags.py" in render
65. result = block.nodelist.render(context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/base.py" in render
844. bit = self.render_node(node, context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/debug.py" in render_node
80. return node.render(context)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/template/debug.py" in render
93. output = force_text(output)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/utils/encoding.py" in force_text
83. s = six.text_type(s)
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/forms/forms.py" in __str__
508. return self.as_widget()
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/forms/forms.py" in as_widget
560. return force_text(widget.render(name, self.value(), attrs=attrs))
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/forms/widgets.py" in render
739. return self.get_renderer(name, value, attrs, choices).render()
File "/Users/johnking/workspace/rice-bikes/py3env/lib/python3.4/site-packages/django/forms/widgets.py" in render
687. choice_value, choice_label = choice
Exception Type: ValueError at /create_order/
Exception Value: too many values to unpack (expected 2)
答案 0 :(得分:1)
您的choices
应该是两个项目的迭代列表。
您可以这样做:
FRAME_AND_ALIGNMENT_CHOICES = [
("Align Derailleur Hanger", "Align Derailleur Hanger")
("Basic Clean", "Basic Clean"),
("Install Front Basket", "Install Front Basket"),
]
或使用列表理解来减少重复:
FRAME_AND_ALIGNMENT_CHOICES = [(c, c) for c in ["Align Derailleur Hanger", "Basic Clean", "Install Front Basket"]]
或者,您可能希望在数据库中存储代码,并向用户显示完整描述:
FRAME_AND_ALIGNMENT_CHOICES = [
("derailleur", "Align Derailleur Hanger"),
("clean", Basic Clean"),
("basket", "Install Front Basket"),
]