我正在尝试使用复选框创建多项选择。我在复选框中显示了数据,但是当我提交时出现以下错误:
模板错误:要解压的值太多
我读到有些人的问题是他们没有创建 2tuples 作为选择列表的元素。但情况似乎并非如此。问题可能是什么?
表格.py
class Test(forms.Form):
answer = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
def __init__(self, options, *args, **kwargs):
super(Test, self).__init__(*args, **kwargs)
self.fields['answer'].choices = options
视图.py
def multiChoice(request,ex):
multi = MultipleChoice.objects.get(pk=ex)
choices = multi.correct_choices.all() | multi.wrong_choices.all()
if request.method == 'POST':
form = Test(request.POST)
if form.is_valid():
multiple = form.save()
return HttpResponseRedirect('/edu/multi/1')
else:
form = Test(options=[( choice.id , choice ) for choice in choices])
return render(request,'edu/multi.html', {'form': form, 'multi': multi , 'choices': choices})