formset有效但表单没有属性cleaning_data!

时间:2011-05-12 15:14:19

标签: django django-templates django-forms

好的,所以我有一个有效的formset。但是给我一个错误,那个表单没有属性cleaning_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>

如果我评论调用cleaning_data的行(choice1),我没有收到任何错误,我可以看到表格..
如果我选择一些选项并取消注释这一行,它就可以工作..

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

有效的表单是第一个formset(上面链接)。帖子参数:

form-0-pres_name    1
form-1-pres_name    2
form-INITIAL_FORMS  0
form-TOTAL_FORMS    2

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

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

如果有人需要,以下是我在视图中所做的修改:

 ClinicalSet = formset_factory(Clinical, extra=tamanh)

 #size of this list will determine number of forms
 wrongs = iter(wrong)
 formset = ClinicalSet(request.POST)
 dic = {}
 if formset.is_valid():
     if len(request.POST.keys()) == 0:
         return render_to_response('valid.html',  
                                        {
                                            'formset': formset,
                                            'wrongs': wrongs })
     else:
         #+ 2 because of TOTAL_FORMS and MAX_NUM_FORMS
         if len(request.POST.keys()) != len(wrongs) + 2:
             msg = "You have to select something in all forms!!"
             return render_to_response('valid.html', 
                                            {
                                                'formset': formset,
                                                'wrongs': wrongs,
                                                'msg': msg })
         else:
             for n in range(tamanh):
                 #result post into a dictionary, since cleaned_data doesn't work
                 dic[wrongs.next()] = request.POST['form-' + str(n) + '-cliform_name']
                 return HttpResponseRedirect(reverse('valid2', args=[word]))
    else:
        formset = ClinicalSet()
    return render_to_response('valid.html', 
                                {
                                    'formset': formset,
                                    'wrongs': wrongs,
                                    'msg': msg
                                    })