使用动态内联表单集的Django

时间:2014-04-24 18:39:19

标签: python django

我要使用 here 代码。

问题是我收到错误" ManagementForm数据丢失或被篡改过"我随时尝试向页面添加新表单。

我的问题几乎是肯定的:{{formset.management_form}}

以下是我正在使用的内容:

{% extends "base.html" %}

{% block base_content %}

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="{{ STATIC_URL }}js/jquery.formset.js"></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>

<form id="myForm" method="post">
{% csrf_token %}
<table>
    {{ form.as_table }}
</table>
{% for formset in inlines %}
    <div id="FormSet{{ formset.prefix }}">
    {% for subform in formset.forms %}
        <table>
        {{ subform.as_table }}
        </table>
    {% endfor %}
    {{ formset.management_form }}
    </div>
{% endfor %}

<input type="submit" value="Save new tool calibration">
</form>

{% endblock %}

Forms.py

from django.forms import ModelForm
from extra_views import InlineFormSet, CreateWithInlinesView, UpdateWithInlinesView
from extra_views.generic import GenericInlineFormSet
from tool_cal.models import ToolCalibration, SerialNumber, PartNumber

class ToolForm(ModelForm):
    lass Meta:
        model = ToolCalibration


class SerialFormSet(InlineFormSet):
    model = SerialNumber
    extra = 1
    prefix = "serial"


class PartFormSet(InlineFormSet):
    model = PartNumber
    extra = 1
    prefix = "part"

感谢任何和所有帮助!

0 个答案:

没有答案