简体   繁体   English

如何将类添加到 django-crispy-forms 中的表单集中的子表单

[英]How to add classes to subforms within formset in django-crispy-forms

What I'm after is the ability to style forms within my formset:我所追求的是能够在我的表单集中设置表单样式:

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

Is there a way to do this cleanly with the FormHelper (as for adding submit buttons etc.) or do I need to do it in my template and manually loop over the formset?有没有办法使用 FormHelper 干净利落地执行此操作(如添加提交按钮等),还是我需要在模板中执行此操作并手动遍历表单集?

You can use layout.HTML() block to render the inline forms there like this:您可以使用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)

Each of your inline forms can have the helper with its own layout.您的每个内联表单都可以拥有具有自己布局的帮助程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM