Django Formsets:ManagementForm数据丢失或被篡改

时间:2012-11-22 09:06:50

标签: django django-forms

我正在使用内联表单集。我有一个主要表单应用程序。然后2个formsets 使用 qual

当我运行我的观点时。我收到错误消息 ManagementForm数据丢失或已被篡改

这是我的观点

@login_required
def apply(request):
    if request.POST:
        data = {'form-TOTAL_FORMS': u'1','form-INITIAL_FORMS': u'0','form-MAX_NUM_FORMS': u''}
        form = ApplicationForm(request.POST)
        employ_formset = EmploymentFormSet(data)
        qual_formset = QualificationFormSet(data)
        if form.is_valid():
            application = form.save(commit=False)
            employ_formset = EmploymentFormSet(request.POST, instance=application)
            qual_formset = QualificationFormSet(request.POST, instance=application)
            if employ_formset.is_valid() and qual_formset.is_valid():
                application.save()
                employ_formset.save()
                qual_formset.save()
            return HttpResponse('This Worked!!!!!!!!')
    else:
        form = ApplicationForm()
        employ_formset = EmploymentFormSet(instance=Application())
        qual_formset = QualificationFormSet(instance=Application())
    return render_to_response('apply.html', {'form':form, 'employ_formset':employ_formset, 'qual_formse

这是我的模板。我意识到我可以简单地模板。但我希望内联表单集像Django Admin一样水平显示。

{% extends "base.html" %}
{% load i18n %}

{% block content %}
<form action="." method="post" class="application_form">
    {% csrf_token %}
    <!-- PERSONAL -->
    <h3>Applicant Details</h3><br />
    <table>
      <tr>
        <td><label for="id_title">Title:</label></td>
        <td>{{ form.title }}<small>{{ form.title.errors }}</small></td>
      </tr>
      <tr>
        <td><label for="id_firstname">Firstname:</label></td>
        <td>{{ form.firstname }}<small>{{ form.firstname.errors }}</small></td>
        <td><label for="id_surname">Surname:</label></td>
        <td>{{ form.surname }}<small>{{ form.surname.errors }}</small></td>
      </tr>
      <tr>
        <td><label for="id_address">Address:</label></td>
        <td>{{ form.address }}<small>{{ form.address.errors }}</small></td>
      </tr>
      <tr>
        <td><label for="id_email">Email:</label></td>
        <td>{{ form.email }}<small>{{ form.email.errors }}</small></td>
      </tr>
      <tr>
        <td><label for="id_mobilephone">Mobile Phone:</label></td>
        <td>{{ form.mobilephone }}<small>{{ form.mobilephone.errors }}</small></td>
      </tr>
      <tr>
        <td><label for="id_homephone">Home Phone:</label></td>
        <td>{{ form.homephone }}<small>{{ form.homephone.errors }}</small></td>
      </tr>
    </table>

    <!-- EDUCATION -->
    <br />
    <h3>Employment Details</h3><br />
    {{ employ_formset.management_form }}
    {{ employ_formset.non_form_errors.as_ul }}
    <table id="formset" class="form">
    {% for form in employ_formset.forms %}
      {% if forloop.first %}
      <thead>
        <tr>
        {% for field in form.visible_fields %}
          <th>{{ field.label|capfirst }}</th>
        {% endfor %}
        </tr>
      </thead>
      {% endif %}
      <tr class="{% cycle row1,row2 %}">
      {% for field in form.visible_fields %}
    <td>
    {# Include the hidden fields in the form #}
    {% if forloop.first %}
      {% for hidden in form.hidden_fields %}
        {{ hidden }}
      {% endfor %}
    {% endif %}
    {{ field.errors.as_ul }}
    {{ field }}
    </td>
  {% endfor %}
  </tr>
{% endfor %}
</table>

1 个答案:

答案 0 :(得分:1)

您不必像在代码中那样构建管理数据的字典:

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

这应该来自POST。如果需要,可以在处理GET请求时构建formset时使用max_num, extra参数进行适当更改。