gpt4 book ai didi

javascript - 使用 django-dynamic-formset 创建表单集工厂时出错

转载 作者:行者123 更新时间:2023-12-03 01:55:33 27 4
gpt4 key购买 nike

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

我的表单.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对于一个(或两个)表单集。

关于javascript - 使用 django-dynamic-formset 创建表单集工厂时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50260414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com