代码之家  ›  专栏  ›  技术社区  ›  Roger

django动态多回波场的处理

  •  2
  • Roger  · 技术社区  · 16 年前

    到目前为止,我看到的所有答案都把我弄糊涂了。

    多重冰原让我特别困惑。我把它们定义为:

    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]
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   Daniel Roseman    16 年前

    ... 这不管用。。。

    你确定?你测试过了吗?通常 cleaned_data

    所以是的,它只返回问题1一次,但是返回的值本身包含多个值。