gpt4 book ai didi

jquery - 将 django-dynamic-formset 与来自 d​​jango-extra-views 的 CreateWithInlinesView 一起使用 - 多个表单集

转载 作者:太空狗 更新时间:2023-10-29 21:07:17 25 4
gpt4 key购买 nike

我有 3 个模型:

class Client(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=40)

class Phone(models.Model):
number = models.CharField(max_length=10)
client = models.ForeignKey(Client)

class ClientEmail(models.Model):
client = models.ForeignKey(Client)
address = models.EmailField(verbose_name='Email')

一个表单和两个内联表单集:

class ClientForm(ModelForm):
class Meta:
model = Client


class PhoneFormSet(InlineFormSet):
model = Phone
extra = 1


class EmailFormSet(InlineFormSet):
model = ClientEmail
extra = 1

查看:

class ClientCreateView(LoginRequiredMixin, CreateWithInlinesView):
model = Client
inlines = [PhoneFormSet, EmailFormSet,]

和工作模板:

{% extends 'base.html' %}
{% block extra_head_script %}
<script src="{{ STATIC_URL }}js/jquery.formset.js"></script>
{% endblock %}

{% block content %}
<form action="." method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
{% for formset in inlines %}
<div id="{{ formset.prefix }}">
{% for subform in formset.forms %}
<table>
{{ subform.as_table }}
</table>
{% endfor %}
{{ formset.management_form }}
</div>
{% endfor %}
<input type="submit" value="Add client" class="submit"/>
</form>
{% endblock %}

我刚开始使用 ClassBasedViews,不知道如何使用 django-dynamic-formset js 与 django-extra-views在我的模板中。

最佳答案

我的天啊……我想通了:

{% block extra_footer_script %}
<script type="text/javascript">
$(function() {
{% for formset in inlines %}
$('div#FormSet{{ formset.prefix }}').formset({
prefix: '{{ formset.prefix }}',
formCssClass: 'dynamic-formset{{ forloop.counter }}'
});
{% endfor %}
})
</script>
{% endblock %}

如果您发现任何错误,请指出给我。也感谢任何更好的想法。

对于那些想要使用它的人——是的,它应该可以与任何数量的表单集一起使用

关于jquery - 将 django-dynamic-formset 与来自 d​​jango-extra-views 的 CreateWithInlinesView 一起使用 - 多个表单集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23110929/

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