使用django-dynamic-formset

时间:2018-05-09 19:05:20

标签: javascript python django django-forms formset

我尝试在同一视图中使用两个formset_factory和django动态表单集,但我收到此错误     [DOM]找到2个具有非唯一ID的元素#id_form-TOTAL_FORMS: 当我尝试在第二个表单上添加前缀时,它只生成一个id,然后如果添加多个项目,它将保存最后一个项目。 我想知道如何解决这个问题以及它为什么会发生? 提前谢谢。

my forms.py

class ResearchLineForm(forms.ModelForm):
    class Meta:
        model = ResearchLines
        fields = ('title', )

        widgets = {
            'title':forms.TextInput(attrs={
            'class':'form-control',
            'placeholder':'Linhas de Pesquisa'
        })
    }
LinesFormSet = formset_factory(ResearchLineForm)


class TokenForm(forms.Form):
    name =  forms.CharField( max_length=255, widget=forms.TextInput(
        attrs={
            'class':'form-control',
            'placeholder':'Linhas de Pesquisa'
        })
)
email = forms.CharField(max_length=255, widget=forms.TextInput(
    attrs={
            'class':'form-control',
            'placeholder':'Linhas de Pesquisa'
        })
 )


TokenFormSet = formset_factory(TokenForm)

我的view.py

def ubc_register(request):
    if request.method == "POST":
        ubc = Ubc()
        form = UbcForm(request.POST)
        tokenformset = TokenFormSet(request.POST, request.FILES)
        formset = LinesFormSet(request.POST, request.FILES)
        if form.is_valid():
            ubc.name = form.cleaned_data['name']
            ubc.laboratory_departament = form.cleaned_data['laboratory_departament']
            ubc.partner_category = form.cleaned_data['partner_category']
            ubc.parent_institution_name =  form.cleaned_data['parent_institution_name']
            ubc.coordinator = form.cleaned_data['coordinator']
            if "phone" in form.cleaned_data:
                ubc.phone = form.cleaned_data['phone']
            if "email" in form.cleaned_data:
                ubc.email = form.cleaned_data['email']
            if "cnpj" in form.cleaned_data:
                ubc.cnpj = form.cleaned_data['cnpj']
            if "rad_operating_time" in form.cleaned_data:
                ubc.rad_operating_time = form.cleaned_data['rad_operating_time']
            if "rh_count" in form.cleaned_data:
                ubc.rh_count = form.cleaned_data['rh_count']
            if "website" in form.cleaned_data:
                ubc.website = form.cleaned_data['website']

            ubc.cep = form.cleaned_data['cep']
            ubc.street = form.cleaned_data['street']
            ubc.city = form.cleaned_data['city']
            ubc.neighborhood = form.cleaned_data['neighborhood']
            ubc.number = form.cleaned_data['number']
            ubc.uf = form.cleaned_data['uf']
            ubc.filter_grade = 0
            ubc.grade = 0

            if 'activity_branch' in form.cleaned_data:
                ubc.activity_branch = form.cleaned_data['activity_branch']                 

            ubc.save()

        # research_lines
            if formset.is_valid():
                for researchline in formset.cleaned_data:
                    if bool(researchline):
                        created_researchline = ResearchLines.objects.create(title=researchline['title'])
                        ubc.research_line.add(created_researchline)


            product_ids = [ cert.id for cert in form.cleaned_data["main_product"] ] 
            for main_product in MainProductUbc.objects.filter(id__in=product_ids):
            ubc.main_product.add(main_product)
   )
        return HttpResponseRedirect(reverse('index'))
    else:
        form = UbcForm()
        formset = LinesFormSet()
        tokenformset = TokenFormSet()
    context = {
        'form':form,
        'formset':formset,
        'tokenformset': tokenformset,            
    }
    return render(request, 'accounts/ubc_form.html', context)

我的formset.js文件

$('.link-formset').formset({
   deleteText: 'Remover'
});

1 个答案:

答案 0 :(得分:1)

formsets使用#id_form-TOTAL_FORMS(和其他一些东西)来查看有多少表单并且最初是。

如果您想在一个页面上显示多个表单集,那么您将遇到问题,即您最终得到两个具有相同ID的对象。要解决此问题,您可以为一个(或两个)表单集定义prefix