我们在django项目中使用formset.js来添加或删除表单中的表单。
我无法使删除按钮起作用。
我在formset.js中看到了(这里https://pypi.python.org/pypi/django-formset-js/0.4.0可用)
此代码:
Formset.prototype.bindForm = function($form, index) {
var prefix = this.formsetPrefix + '-' + index;
$form.data(pluginName + '__formPrefix', prefix);
var $delete = $form.find('[name=' + prefix + '-DELETE]');
// Trigger `formAdded` / `formDeleted` events when delete checkbox value changes
$delete.change(function(event) {
if ($delete.is(':checked')) {
$form.attr('data-formset-form-deleted', '');
$form.trigger('formDeleted');
} else {
$form.removeAttr('data-formset-form-deleted');
$form.trigger('formAdded');
}
}).trigger('change');
var $deleteButton = $form.find(this.opts.deleteButton);
$deleteButton.bind('click', function() {
$delete.attr('checked', true).change();
});
};
我的问题是我在代码中找不到任何复选框。模板显示如下:
<div data-formset-body>
<!-- New forms will be inserted in here -->
{% for form in formset %}
<div data-formset-form>
{{ form.as_p }}
<!-- ajout YCO pour progresser dans le delete
<input type="checkbox" name="form-{{ forloop.counter0 }}-DELETE">
-->
<div class="hidden">{{ form.DELETE }}</div>
<a data-formset-delete-button >{% trans "Delete form" %}</a>
<!-- onclick ="$(this).parent().remove();" -->
</div>
{% endfor %}
</div>
有人可以告诉我:
最佳答案
在模板中使用yourform.DELETE
时,必须与can_delete
formset参数一起使用,如下所示。
from django import forms
from django.forms.formsets import formset_factory
class YourForm(forms.Form):
date = forms.DateField()
YourFormSet = formset_factory(YourForm, can_delete = True)
Django document "Formsets"