【问题标题】:How to add classes to subforms within formset in django-crispy-forms如何在 django-crispy-forms 的表单集中向子表单添加类
【发布时间】:2015-09-24 15:54:48
【问题描述】:

我追求的是在我的表单集中设置表单样式的能力:

<form>
    {{ formset management stuff here }}
<div class="formset-child">
    {{ formset child form here }}
</div>
<div class="formset-child">
    {{ formset child form here }}
</div>
</form>

有没有办法用 FormHelper 干净地做到这一点(如添加提交按钮等),还是我需要在我的模板中这样做并手动循环表单集?

【问题讨论】:

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


    【解决方案1】:

    您可以使用layout.HTML() 块在此处呈现内联表单,如下所示:

    layout_blocks = []
    layout_blocks.append(layout.Fieldset(
        _("Children"),
        layout.HTML("""{% load crispy_forms_tags i18n %}
        {{ formsets.children.management_form }}
        <div id="children">
            {% for form in formsets.children.forms %}
                <div class="child formset-form">
                    {% crispy form %}
                </div>
            {% endfor %}
        </div>
        <!-- used by javascript -->
        <div id="children_empty_form" class="child formset-form" style="display: none">
            {% with formsets.children.empty_form as form %}
                {% crispy form %}
            {% endwith %}
        </div>
        """),
        css_id="children_fieldset",
    ))
    layout_blocks.append(bootstrap.FormActions(
       PrimarySubmit('submit', _('Save')),
    ))
    self.helper.layout = layout.Layout(*layout_blocks)
    

    您的每个内联表单都可以拥有具有自己布局的助手。

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 2015-05-13
      • 2021-05-25
      • 2022-01-04
      • 1970-01-01
      • 2015-06-06
      • 2021-01-08
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多