CreateView中的多个表单和表单集

时间:2012-09-24 22:48:11

标签: python django django-forms django-class-based-views

我有2个模型,FatherSon

我有一个页面要注册Father。在同一页面上,我有一个注册Son的表单集。

在页面上有一个“更多”按钮,可以在同一页面上添加另一个Father及其各自的Son

是否有人使用CreateView

有任何示例

1 个答案:

答案 0 :(得分:16)

基于类的视图仍然是新的,所以我会写出来的。这个过程很简单:

首先,为对象创建表单。其中一种形式将重复出现。这里没什么特别的。

class SonInline(ModelForm):
    model = Son

class FatherForm(ModelForm):
    model = Father

然后,创建您的formset

FatherInlineFormSet = inlineformset_factory(Father,
    Son,
    form=SonInline,
    extra=1,
    can_delete=False,
    can_order=False
)

现在,将其与CreateView

集成
class CreateFatherView(CreateView):
    template_name = 'father_create.html'
    model = Father
    form_class = FatherForm # the parent object's form

    # On successful form submission
    def get_success_url(self):
        return reverse('father-created')

    # Validate forms
    def form_valid(self, form):
        ctx = self.get_context_data()
        inlines = ctx['inlines']
        if inlines.is_valid() and form.is_valid():
            self.object = form.save() # saves Father and Children
            return redirect(self.get_success_url())
        else:
            return self.render_to_response(self.get_context_data(form=form))

    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data(form=form))

    # We populate the context with the forms. Here I'm sending
    # the inline forms in `inlines`
    def get_context_data(self, **kwargs):
        ctx = super(CreateFatherView, self).get_context_data(**kwargs)
        if self.request.POST:
            ctx['form'] = FatherForm(self.request.POST)
            ctx['inlines'] = FatherInlineFormSet(self.request.POST)
        else:
            ctx['form'] = Father()
            ctx['inlines'] = FatherInlineFormSet()
        return ctx

最后,这是模板:

关键部分是jquery django-dynamic-formset插件,它不断添加新的内联表单:

<form id="father-form" method="POST" enctype="multipart/form-data" action=".">
{% csrf_token %}
<div class="row">
  {% for f in form %}
    <div class="span3">{{ f.label }}<br />{{ f }}
      {% if f.errors %}
          {% for v in f.errors %}
            <br /><span style="color:red;">{{ v }}</span>
          {% endfor %}
      {% endif %}
    </div>
 {% endfor %}
</div>
<hr />
<h2>Sons:</h2>
<table class="table-striped">
 <table>
 {%  for f2 in inlines %}
   <tr id="{{ f2.prefix }}-row">
      {% for i in f2 %}
        <td>
           {{ i }}{% if i.errors %}<span style="color:red;">{{ i.errors }}</span>{% endif %}
        </td>
      {% endfor %}
   </tr>
 {% endfor %}
</table>
{{ inlines.management_form }}
<input type="submit" class="btn btn-primary" value="Go Go Gadget &rarr;">
</form>
<script type="text/javascript">
    $(function() {
        $('#father-form tr').formset({
            prefix: '{{ inlines.prefix }}'
        });
    })
</script>