使用 django-dynamic-formset 创建 formset 工厂时出错

Error to create formset factory using django-dynamic-formset

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

我的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'
});

表单集使用#id_form-TOTAL_FORMS(以及其他一些东西)来查看有多少表单以及初始表单。

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