1

我正在使用内联表单集。我有一个主窗体应用程序。然后 2 formset使用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>
4

1 回答 1

1

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

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

这应该来自 POST。如果您愿意,可以在构建处理 GET 请求的表单集时使用max_num,参数适当地更改它。extra

于 2012-11-22T09:15:24.820 回答