django crispy forms:在表单中嵌套formset

时间:2013-03-01 12:00:16

标签: django django-forms django-crispy-forms

我有一个django Formset,我想在另一种形式的中间布局。我正在使用django-crispy-forms在父表单__init__中设置布局:

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field, Div
def __init__(self, *args, **kwargs):
    self.helper = FormHelper()
    self.helper.layout = Layout(
        Div(
            Div(Field('foo'), css_class='span3'),
            Div(Field('bar'), css_class='span4'),
            css_class='row'
            ),
        Field('baz', css_class='span1'),
            ...
            )
    self.helper.add_input(Submit('submit', 'Submit', css_class='btn btn-primary offset4'))

我的模板只是使用{% crispy %}标记呈现表单。

我想知道如何合并formset。我应该在上面的init函数中实例化它吗?我怎么在那里引用它?

网上有other examples形式和formset组合,其中一个连续渲染一个渲染,但我想知道我是否可以更好地控制它们如何与crispy的布局结合在一起。

4 个答案:

答案 0 :(得分:18)

我在不修改Crispy Forms的情况下解决了这个问题,创建了一个呈现formset的新字段类型:

from crispy_forms.layout import LayoutObject, TEMPLATE_PACK

class Formset(LayoutObject):
    """
    Layout object. It renders an entire formset, as though it were a Field.

    Example::

    Formset("attached_files_formset")
    """

    template = "%s/formset.html" % TEMPLATE_PACK

    def __init__(self, formset_name_in_context, template=None):
        self.formset_name_in_context = formset_name_in_context

        # crispy_forms/layout.py:302 requires us to have a fields property
        self.fields = []

        # Overrides class variable with an instance level variable
        if template:
            self.template = template

    def render(self, form, form_style, context, template_pack=TEMPLATE_PACK):
        formset = context[self.formset_name_in_context]
        return render_to_string(self.template, Context({'wrapper': self,
            'formset': formset}))

它需要一个模板来渲染formset,它可以让你控制它的呈现方式:

{% load crispy_forms_tags %}

<div class="formset">
    {% crispy formset %}
    <input type="button" name="add" value="Add another" />
</div>

您可以使用它在布局中嵌入formset,就像任何其他Crispy布局元素一样:

self.helper.layout = Layout(
    MultiField(
        "Education",
        Formset('education'),
    ),

答案 1 :(得分:4)

目前酥脆形式不支持此功能。您唯一的选择是使用|as_crispy_field过滤器(尚未记录,抱歉)。

我已开始为{% crispy %}标记开发此功能,并在功能分支中对此进行了解释:https://github.com/maraujop/django-crispy-forms/issues/144

我正在寻求反馈,所以如果您仍然感兴趣,请随时发帖。

答案 2 :(得分:4)

qris稍微修改了之前的答案。

此更新(由Alejandro建议)将允许我们的自定义 Formset 布局对象使用FormHelper对象来控制formset的字段被渲染。

from crispy_forms.layout import LayoutObject

from django.template.loader import render_to_string


class Formset(LayoutObject):
    """ 
    Renders an entire formset, as though it were a Field.
    Accepts the names (as a string) of formset and helper as they
    are defined in the context

    Examples:
        Formset('contact_formset')
        Formset('contact_formset', 'contact_formset_helper')
    """

    template = "forms/formset.html"

    def __init__(self, formset_context_name, helper_context_name=None,
                 template=None, label=None):

        self.formset_context_name = formset_context_name
        self.helper_context_name = helper_context_name

        # crispy_forms/layout.py:302 requires us to have a fields property
        self.fields = []

        # Overrides class variable with an instance level variable
        if template:
            self.template = template

    def render(self, form, form_style, context, **kwargs):
        formset = context.get(self.formset_context_name)
        helper = context.get(self.helper_context_name)
        # closes form prematurely if this isn't explicitly stated
        if helper:
            helper.form_tag = False

        context.update({'formset': formset, 'helper': helper})
        return render_to_string(self.template, context.flatten())

模板(用于渲染formset):

{% load crispy_forms_tags %}

<div class="formset">
  {% if helper %}
    {% crispy formset helper %}
  {% else %}
    {{ formset|crispy }}
  {% endif %}
</div>

现在它可以像任何其他脆弱的表单布局对象一样在任何布局中使用。

self.helper.layout = Layout(
    Div(
        Field('my_field'),
        Formset('my_formset'),
        Button('Add New', 'add-extra-formset-fields'),
    ),
)

# or with a helper
self.helper.layout = Layout(
    Div(
        Field('my_field'),
        Formset('my_formset', 'my_formset_helper'),
        Button('Add New', 'add-extra-formset-fields'),
    ),
)

答案 3 :(得分:0)

基于上面的解决方案Formset(LayoutObject),你可以结合使用django-dynamic-formset&amp;香脆。 在我的订单页面上,我有:

  • 订单的第1部分
  • 使用动态添加表单的订单内联formset
  • 订单的第N部分

现在它简单明了,ModelForms是:

0:        1 ***
1:        2 ******
2:        3 ********
3:        3 ********
4:        5 **************
5:        1 ***
6:        2 ******
7:        1 ***
8:        1 ***

Formset助手布局:

class OrderTestForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
    super(OrderTestForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper(self)
    self.helper.form_tag = True
    self.helper.html5_required = True
    self.helper.form_action = 'test_main'
    self.helper.layout = Layout(
        'product_norms', #section 1
        'reference_other', #section 1
        # rest of the section 1 fields
        Formset('samples', 'helper'), # inline dynamic forms
        'checkbox_is_required' # start of section N
        # other order sections fields
    )
    self.helper.add_input(Submit("submit", "Save order"))

添加/删除内联,使用formset操作保存顺序按预期工作。