django - formset 有效但表单没有属性cleaned_data!

标签 django django-templates django-forms

好的,所以我有一个有效的表单集。但是给了我一个错误,该表单没有属性cleaned_data ..

老实说,我完全不知道发生了什么..
我在终端上尝试了我的代码,它返回了一个空字典..没有错误..

形式:

class Clinical(forms.Form):
    _names = list(ClinicalForm.objects.values_list('form_id', 'form_name'))
    _names.append(("New", u'Nova entrada'))

    cliform_name = forms.ChoiceField(widget=RadioSelect(), choices=_names, label
         ="", required=True)

意见:
ClinicalSet = formset_factory(Clinical, extra=2)

formset2 = ClinicalSet(request.POST)
if formset2.is_valid():
    choice1 = formset2.cleaned_data
    return render_to_response('valid_test.html', 
                                {
                                    'formset2': formset2,
                                    'wrongs1': wrongs1,
                                    'choice1': choice1 
                                    })
else:
    formset2 = ClinicalSet()
return render_to_response('valid_test.html', 
                            {
                                'formset2': formset2,
                                'wrongs1': wrongs1,
                                })    

模板:
<form method="post" action="">
<div>
{{ formset2.management_form }}
    {% for form in formset2.forms %}
        {{ form }}
    {% endfor %} 
    <input type="submit" value="save" />
</div>



如果我评论了调用clean_data(选择1)的行,我不会收到任何错误并且我能够看到表单..
如果我选择一些选项并取消注释这一行,它就可以工作..

我有一个类似的表单集:both forms in formset need to be selected这个有效..

有效的表格是第一个表格集(上面链接)。帖子参数:
form-0-pres_name    1
form-1-pres_name    2
form-INITIAL_FORMS  0
form-TOTAL_FORMS    2

用户在每个表单中选择一个选项,然后他重定向到另一个 View (这个 - formset2)。

任何帮助都非常受欢迎..

最佳答案

formset_factory返回一个表单迭代器,即本质上是一个表单列表,它不是表单本身。 cleaned_data仅在表单上可用,因此您必须遍历 formset2 :

for form in formset2:
    form.cleaned_data # Here I am!

关于django - formset 有效但表单没有属性cleaned_data!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5980251/

相关文章:

python - google-app-engine-django 加载装置

django 通过 HttpResponse 返回文件 - 文件未正确提供

python - FieldError 在/。无法将关键字 'textField' 解析为字段

django:如何仅访问 form.errors 中的第一条错误消息?

django - 如何在django中找回密码

Python 线程不返回值

django - 如何检查特定 URL 保留匹配是否有效?

django - 如何在 django 模板中重复 "block"

python - 从 django 表单获取图片 url

django - 将 django 网站运行到服务器时,“模块”对象不可迭代