到目前为止,我看到的所有答案都把我弄糊涂了。
多重冰原让我特别困惑。我把它们定义为:
self.fields['question_' + str(question.id)] = forms.MultipleChoiceField(
label=mark_safe(required_tag +
question.label + "<br/>Choose any of the following answers"),
help_text=question.description,
required=question.required,
choices=choices,
widget=widgets.CheckboxSelectMultiple())
当我选择几个选项时,实际发布的数据如下所示:
question_1=5&question_1=6
django是否会自动意识到这两个选项在同一个窗体上,并让我在某个地方访问iterable?我要做的是:
for field in self.cleaned_data:
print field # save the user's response somehow
question_1
一次,即使有两个提交的值。
:如果我循环通过,for循环现在可以按预期工作
self.fields
而不是
self.cleaned_data
:
for field in self.fields:
print self.cleaned_data[field]