'ManagementForm data is missing or has been tampered with' 姜戈

'ManagementForm data is missing or has been tampered with' Django

我创建了一个 formset 供用户使用 Django 的 empty_formjQuery 根据需要添加和删除表单。但是,当 form 被提交时,我被抛出一个验证错误。我已经初始化了 ManagementForm,所以我不确定问题出在哪里。任何帮助将不胜感激。

views.py

def presales(request):

    PresalesFormSet = formset_factory(PresalesForm, extra=1, can_delete=True)

    if request.method == 'POST':

        presales_formset = PresalesFormSet(request.POST)

        if presales_formset.is_valid():
            selected_opportunity = request.POST.get('selected_opportunity')

            for presales_form in presales_formset:

                task_description = request.POST.get('task_description')
                hours = request.POST.get('hours')

                obj = Presales()
                obj.opportunity = selected_opportunity
                obj.taskDescription = task_description
                obj.hours = hours
                obj.save()

    else:
        presales_form = PresalesForm()
        presales_formset = PresalesFormSet()

    context = {'presales_formset': presales_formset, 'presales_form': presales_form, 'my_opportunities': my_opportunities}

    return render(request, 'website/presales.html', context)

template.html

{{ presales_formset.management_form }}
    <form action="{% url 'presales' %}" class="presales_formset" data-total-url="{% url 'presales_total' %}" id="presalesForm"
                  method="post" name="presalesForm">
        <div class="field">
            <label class="label is-large">High Level Task List</label>
        </div>
        {% csrf_token %}

        {% for presales_form in presales_formset.forms %}

            {{ presales_form.field_errors }}
            {{ presales_form.errors }}
    </form>
    {% endfor %}
    <div id="empty_form" style="display: none;">
    {{ presales_formset.empty_form }}

        <form action="{% url 'presales' %}" class="presales_formset" data-total-url="{% url 'presales_total' %}" id="emptyPresalesForm"
                  method="post" name="presalesForm">
        {% csrf_token %}
        </form>
    </div>

forms.py

# special field names
TOTAL_FORM_COUNT = 'TOTAL_FORMS'
INITIAL_FORM_COUNT = 'INITIAL_FORMS'
MIN_NUM_FORM_COUNT = 'MIN_NUM_FORMS'
MAX_NUM_FORM_COUNT = 'MAX_NUM_FORMS'
ORDERING_FIELD_NAME = 'ORDER'
DELETION_FIELD_NAME = 'DELETE'

# default minimum number of forms in a formset
DEFAULT_MIN_NUM = 0

# default maximum number of forms in a formset, to prevent memory exhaustion
DEFAULT_MAX_NUM = 1000


class PresalesForm(forms.Form):
    class Meta:
        model = Presales
        fields = ('selected_opportunity', 'task_description', 'hours', 'selected_engineer_level', 'total_price')

management_form 应该在您的表单集中,例如:

<form action="{% url 'presales' %}" class="presales_formset" data-total-url="{% url 'presales_total' %}" id="presalesForm"
              method="post" name="presalesForm">
    {{ presales_formset.management_form }}
    <div class="field">
        <label class="label is-large">High Level Task List</label>
    </div>
    {% csrf_token %}

    {% for presales_form in presales_formset.forms %}

        {{ presales_form.field_errors }}
        {{ presales_form.errors }}
</form>

您需要一些额外的数据:form-TOTAL_FORMSform-INITIAL_FORMSform-MAX_NUM_FORMSManagementForm 需要此数据。您似乎没有提供此管理数据。它应该是这样的:

data = {
    'form-TOTAL_FORMS': '1',
    'form-INITIAL_FORMS': '0',
    'form-MAX_NUM_FORMS': '',
    'form-0-title': '',
    'form-0-pub_date': '',
}