在formset中动态添加表单而不是保存/管理表单手动更新?

时间:2017-02-21 23:29:08

标签: jquery python django forms

我一直关注此tutorial,表单复制/删除部分工作正常,但只保存了第一个表单。从formset的data,我知道值存在以及form-TOTAL_FORMS应该是2(或多少)而不是1.我认为这就是geo_form.cleaned_data所做的不包含任何第二种形式。我假设如果form-TOTAL_FORMS匹配,那么所有表格数据都会在那里。如果这是正确的,我该如何更新管理表格?如果有另一种解决方案,我将如何制定?

(Pdb) for k, v in geo_formset.data.items(): print k, v
form-0-tables test1, test2, test3
form-0-name Shelburne, Burlington, Rutland
form-1-state OR
outfile x.xlsx
form-MAX_NUM_FORMS 1000
form-0-survey ACS1
form-1-name Washington, Clackamas
form-TOTAL_FORMS 1
form-0-years 2000
form-MIN_NUM_FORMS 0
form-INITIAL_FORMS 0
form-1-region county
form-0-state VT
form-0-region place

以下是观点:

class Start(View):

    def get(self, request):
        GeoFormset = formset_factory(GeographyForm)
        CensusFormset = formset_factory(forms.CensusForm)
        geo_formset = GeoFormset()
        cen_formset = CensusFormset()
        out_form = forms.OutfileForm()
        return render(
            request,
            'start.html',
            {
                'geo_formset': geo_formset,
                'cen_formset': cen_formset,
                'out_form': out_form
            }
        )

    def post(self, request):
        GeoFormset = formset_factory(GeographyForm, min_num=1, validate_min=True, extra=0)
        CensusFormset = formset_factory(forms.CensusForm, min_num=1, validate_min=True, extra=0)
        geo_formset = GeoFormset(request.POST)
        cen_formset = CensusFormset(request.POST)
        out_form = forms.OutfileForm(request.POST)
        if geo_formset.is_valid() and out_form.is_valid() and cen_formset.is_valid():
            import pdb; pdb.set_trace()
            return HttpResponseRedirect(reverse('completed'))
        else:
            return render(
                request,
                'start.html',
                {
                    'geo_formset': geo_formset,
                    'cen_formset': cen_formset,
                    'out_form': out_form
                }
            )

和模板

{% extends "base.html" %}

{% block body %}
  <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

  <form method="post" action="">{% csrf_token %}
    {{cen_formset.management_form}}
    {% for cen_form in cen_formset.forms %}
      <fieldset>
        <div class="census-formset">
          Select a survey: {{ cen_form.survey }} | {{ cen_form.tables }}<br>
          {{ cen_form.years }}
        </div>
      </fieldset>
    {% endfor %}
    <br>
    {{geo_formset.management_form}}
    {% for geo_form in geo_formset %}
      <fieldset>
        <div class="geo-formset">
          {{ geo_form.as_p }}
        </div>
      </fieldset>
    {% endfor %}
    <br>
    <fieldset>
      {{ out_form.as_p }}
    </fieldset>

    <input type="submit" value="OK">
  </form>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.formset/1.2.2/jquery.formset.min.js"></script>
  <style type="text/css">
    .remove_text{
      clear: left;
      color: red;
    }
  </style>
  <script type="text/javascript">
    $('.census-formset').formset({
        addText: 'add census',
        deleteText: 'remove',
        deleteCssClass: 'remove_text',
    });
    $('.geo-formset').formset({
        addText: 'add regions',
        deleteText: 'remove',
        deleteCssClass: 'remove_text',
    });
</script>
{% endblock %}

注意:我使用的是Django 1.10.5

2 个答案:

答案 0 :(得分:1)

要在一个视图中拥有多个formset,您需要在实例化formset时设置前缀参数(see docs)。否则会出现命名冲突。

即。 geo_formset = GeoFormset(request.POST)geo_formset = GeoFormset(request.POST, prefix='geo')CensusFormset为{。}}。

答案 1 :(得分:0)

我将接受@neomaic的答案作为公认的答案,因为它让我得到了正确的答案。除了将prefix添加到我的表单集之外,我还必须更新JQuery

<script type="text/javascript">
    $('.census-formset').formset({
        prefix: "{{cen_formset.prefix}}",
        addText: 'add census',
        deleteText: 'remove',
        deleteCssClass: 'remove_text',
    });
    $('.geo-formset').formset({
        prefix: "{{geo_formset.prefix}}",
        addText: 'add regions',
        deleteText: 'remove',
        deleteCssClass: 'remove_text',
    });
 </script>