表单集有效,但表单没有属性cleaned_data! [英] formset is valid but form has no attribute cleaned_data!

查看:78
本文介绍了表单集有效,但表单没有属性cleaned_data!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



老实说,我完全不知道发生了什么。.

我尝试了我的代码在终端上,它返回了一个空字典。.没有错误。。



表格:

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

cliform_name = form.ChoiceField(widget = RadioSelect(),choices__names,标签
=,required = True)

视图:

  ClinicalSet = formset_factory(Clinical,extra = 2)

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

模板:

 < form method = post action => 
< div>
{{formset2.management_form}}
{%for formset2.forms%}
{{form}}
{%endfor%}
< input type = submit value =保存 />
< / div>



的cleaned_data称为(choice1),我没有收到任何错误,我可以看到表格。.

如果我选择了一些选项并取消了对此行的注释,则可以正常工作。 >

我有一个相似的表单集:都需要选择表单集中的两个表单,这个表单才能起作用。



起作用的表单是第一个表单集(上面链接)。 post参数:

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

用户在每种表单中选择一个选项,然后重定向



任何帮助都非常受欢迎。

解决方案

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

  for formset2中的表单:
form.cleaned_data#我在这里!


Ok, so I have a formset that is valid. But gives me a error that that form has no attribute cleaned_data..

Honestly I have absolutely no clue what's happening..
I tried my code on terminal and it returned a empty dictionary.. without errors..

forms:

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)

views:

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,
                                })    

template:

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

If I comment the line where the cleaned_data is called (choice1), I don't receive any error and I'm able to see the forms..
If I select some options and uncomment this line, it works..

I have a similar formset : both forms in formset need to be selected and this one works..

the form that works is the first formset (linked above). The post parameters:

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

the user select one option in each form and he's redirect to another view (this one - formset2).

Any help is more than welcome..

解决方案

formset_factory returns a form iterator, i.e. essentially a list of forms, it is not a form itself. cleaned_data is only available on the form, so you have to iterate over formset2:

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

这篇关于表单集有效,但表单没有属性cleaned_data!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆