需要选择formset中的两个表单 [英] both forms in formset need to be selected

查看:201
本文介绍了需要选择formset中的两个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



表单:

  class PresClinicForm(forms.Form):
_names = list(PresClinic.objects.values_list('pres_clinic_id','pres_clinic_name'))
_names.append([New,u'Nova entrada'])

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

PresClinicFormSet = formset_factory(PresClinicForm,extra = 2)

视图:

  if request.method =='POST':
formset1 = PresClinicFormSet(request.POST,request.FILES,prefix ='pres_clinic')
如果formset1.is_valid():
choice = formset1.cleaned_data
return render_to_response('template.html',{'options':options})
pre>

模板:

 < form method =post行动=  > 
< div>
{{formset1.management_form}}
{form for formset1.forms%}
{{form}}
{%endfor%}
<输入type =submitvalue =Guardar/>
< / div>
< / form>

用户必须在每个表单中选择一个选项。

我尝试了= True into forms.py,但是如果我只选择一个选项,它的工作原理。



如果用户只选择一个选项,它不应该工作。这是我想要实现的..



有人知道如何做吗?



提前感谢任何帮助!

解决方案

请检查Mark的问题:
Django:需要所有的formset表单?



小心,你只能使用formset与django 1.3,与django 1.1 clean_data给出一个错误。


I have a formset that have two forms.

forms:

class PresClinicForm(forms.Form):
    _names = list(PresClinic.objects.values_list('pres_clinic_id', 'pres_clinic_name'))
    _names.append(["New", u'Nova entrada'])

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

PresClinicFormSet = formset_factory(PresClinicForm, extra=2)

views:

if request.method == 'POST':
    formset1 = PresClinicFormSet(request.POST, request.FILES, prefix='pres_clinic')
    if formset1.is_valid():
        choice = formset1.cleaned_data
    return render_to_response('template.html', {'options': options})

template:

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

The user has to select one option in each form.
I tried required=True into forms.py but if I select only one option, it works anyway..

It shouldn't work if the user only select an option. That's what I trying to implement..

Does someone knows how to do this?

Thanks in advance for any help!

解决方案

Please check Mark's question for this answer: Django: Make all formset forms required?

Be careful, you can only use formset with django 1.3, with django 1.1 cleaned_data gives an error.

这篇关于需要选择formset中的两个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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