gpt4 book ai didi

javascript - 如何将 Django inlineformset_factory 与自定义表单集一起使用

转载 作者:行者123 更新时间:2023-12-03 11:32:11 26 4
gpt4 key购买 nike

我想将 Django inlineformset_factory 与自定义表单集一起使用,并希望使用 django-dynamic-formset 添加或删除该内联表单集.

表格:

class CreatePacketForm(forms.ModelForm):
"""
CreatePacketForm class
"""

class Meta:

model = Packet
exclude = ('customer', 'created_on', 'updated_on',
'created_by', 'updated_by', 'remark', 'p_id'
)

ItemFormSet = inlineformset_factory(Packet, Item, form=CreatePacketForm,
formset=CreateItemForm,
extra=1, max_num=20, can_delete=False)

查看:

class CreatePacketView(LoginRequiredMixin, CreateView):
"""
"""

model = Packet
form_class = CreatePacketForm
template_name = CREATE_PACKET_TEMPLATE

def get(self, request, *args, **kwargs):
"""
Handles GET requests and instantiates blank versions of the form
and its inline formsets.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
item_form = ItemFormSet()
return self.render_to_response(self.get_context_data(form=form,
item_form=item_form,))

模板:

{% load static %}

<!DOCTYPE html>
<html>
<head>
<title>Popup</title>
</head>
<body>
<div style="border: 1px solid;padding: 10px;">
<br>
<div align="middle">
<span style="font-size: x-large;">Please Enter Item Details</span>
</div>
<br>
<form id="packet_form" method="post" action="" style="margin: 0 20% 0 20%;padding: 10px;">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form }}
{{ item_form.management_form }}
{{ item_form.non_form_errors }}
{% for subform in item_form %}
<div class="inline {{ item_form.prefix }}">
{{ subform }}
</div>
{% endfor %}
<div align="middle">
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</div>
</form>
</div>
<script type="text/javascript" src="{% static 'girvi/js/jquery-1.3.2.min.js' %}"></script>
<script type="text/javascript" src="{% static 'girvi/js/jquery.formset.js' %}"></script>
<script type="text/javascript" src="{% static 'girvi/js/RelatedObjectLookups.js' %}"></script>
<script type="text/javascript" src="{% static 'girvi/js/swfobject.js' %}"></script>
<script type="text/javascript">
$(".inline.{{ item_form.prefix }}").formset({
prefix: "{{ item_form.prefix }}"
});
</script>
</body>
</html>

我的内联表单集在没有自定义表单集的情况下工作正常。但是当我在模板中渲染它时,它没有 management_form 并且我的表单集中的所有字段都可以被删除。

没有自定义表单集的渲染模板

<!DOCTYPE html>
<html>
<head></head>
<body>
<div style="border: 1px solid;padding: 10px;">
<br></br>
<div align="center"></div>
<br></br>
<form id="packet_form" style="margin: 0 20% 0 20%;padding: 10px;" action="" method="post">
<input type="hidden" value="u3H6thNcyymuwQDrLf5Spnr6E6DEYTxA" name="csrfmiddlewaretoken"></input>
<input id="id_item_set-TOTAL_FORMS" type="hidden" value="1" name="item_set-TOTAL_FORMS"></input>
<input id="id_item_set-INITIAL_FORMS" type="hidden" value="0" name="item_set-INITIAL_FORMS"></input>
<input id="id_item_set-MIN_NUM_FORMS" type="hidden" value="0" name="item_set-MIN_NUM_FORMS"></input>
<input id="id_item_set-MAX_NUM_FORMS" type="hidden" value="20" name="item_set-MAX_NUM_FORMS"></input>
<input id="id_item_set-0-id" type="hidden" name="item_set-0-id"></input>
<div class="inline item_set dynamic-form"></div>
<a class="add-row" href="javascript:void(0)"></a>
<div align="center"></div>
</form>
</div>
<script src="/static/girvi/js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/static/girvi/js/jquery.formset.js" type="text/javascript"></script>
<script src="/static/girvi/js/RelatedObjectLookups.js" type="text/javascript"></script>
<script src="/static/girvi/js/swfobject.js" type="text/javascript"></script>
<script type="text/javascript"></script>
</body>
</html>

以及带有自定义表单集的模板

<!DOCTYPE html>
<html>
<head></head>
<body>
<div style="border: 1px solid;padding: 10px;">
<br></br>
<div align="center"></div>
<br></br>
<form id="packet_form" style="margin: 0 20% 0 20%;padding: 10px;" action="" method="post">
<input type="hidden" value="u3H6thNcyymuwQDrLf5Spnr6E6DEYTxA" name="csrfmiddlewaretoken"></input>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<div class="inline None dynamic-form"></div>
<a class="add-row" href="javascript:void(0)"></a>
<div align="center"></div>
</form>
</div>
<script src="/static/girvi/js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/static/girvi/js/jquery.formset.js" type="text/javascript"></script>
<script src="/static/girvi/js/RelatedObjectLookups.js" type="text/javascript"></script>
<script src="/static/girvi/js/swfobject.js" type="text/javascript"></script>
<script type="text/javascript"></script>
</body>
</html>

without formset enter image description here

最佳答案

您的问题

ItemFormSet = inlineformset_factory(Packet, Item, form=CreatePacketForm,
formset=CreateItemForm,
extra=1, max_num=20, can_delete=False)

form kwarg 这里是自定义formset表单

所以应该是:

ItemFormSet = inlineformset_factory(Packet, Item, form=CreateItemForm, extra=1, max_num=20, can_delete=False)

关于javascript - 如何将 Django inlineformset_factory 与自定义表单集一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26690969/

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