【问题标题】:Inlineformset with crispy forms - display labels once only带有清晰表单的内联表单集 - 仅显示一次标签
【发布时间】:2021-01-31 15:29:48
【问题描述】:

我有一个内联表单集,其中包含清晰的表单。它工作得很好,但我得到每行上方显示的标签。我更喜欢它们一次,在字段表的顶部,但不太确定如何实现这一点。

我定义了一个布局对象,我试图关闭标签以开始,但我猜辅助对象不能以这种方式工作......

class Formset(LayoutObject):
    template = "missions/formset.html"

    def __init__(self, formset_name_in_context, template=None):
        self.formset_name_in_context = formset_name_in_context
        self.fields = []
        if template:
            self.template = template

        self.helper = FormHelper()
        self.helper.form_show_labels = False

    def render(self, form, form_style, context, template_pack=TEMPLATE_PACK):
        formset = context[self.formset_name_in_context]
        return render_to_string(self.template, {"formset": formset})class Formset(LayoutObject):
    template = "missions/formset.html"

在主窗体中使用如下:

        self.helper.layout = Layout(
                < snipped the main form parts as not relevant to the question >
                Div(
                    Fieldset(
                        _("Invitees"),
                        Field("allow_invitees_to_add_others", css_class="col-md-12"),
                        Formset("invitees"),
                    ),
                ),
            )
        )

在我的 formset.html 中

<table>
    {{ formset.management_form|crispy }}
    {% for form in formset.forms %}
    <tr class="{% cycle 'row1' 'row2' %} formset_row-{{ formset.prefix }}">
        {% for field in form.visible_fields %}
        <td>
            {# Include the hidden fields in the form #}
            {% if forloop.first %}
            {% for hidden in form.hidden_fields %}
            {{ hidden }}
            {% endfor %}
            {% endif %}
            {{ field.errors.as_ul }}
            {{ field|as_crispy_field }}
        </td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>

所以我想知道:

  1. 如何关闭表单集中每一行的标签。
  2. 然后如何在 {% for %} 循环之前循环遍历模板中的标签,这样我只需显示一次。

编辑: 好的,所以我已经处理了将顶部标题放入,但仍然不知道如何关闭表单集中每个表单的标题...

    {% if forloop.first %}
    <thead>
        <tr>
            {% for field in form.visible_fields %}
            <th scope="col">
                {{ field.label }}
            </th>
            {% endfor %}
        </tr>
    </thead>
    {% endif %}

【问题讨论】:

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


    【解决方案1】:

    好的,经过一番折腾,我终于解决了这个问题,尽管标签隐藏可能有点“hacky”。​​

    如我所说,标题行的显示可以在模板中处理:

        {% if forloop.first %}
        <thead>
            <tr>
                {% for field in form.visible_fields %}
                <th scope="col">
                    {{ field.label }}
                </th>
                {% endfor %}
            </tr>
        </thead>
        {% endif %}
    

    虽然关闭标签可以在清晰的表单布局对象的定义中完成,并且需要在表单集中的每个表单上单独设置助手(我担心这个循环可能有点hacky,但我只会有这里有少量记录,所以它不应该是一个巨大的性能开销)。

    class Formset(LayoutObject):
        template = "missions/formset.html"
    
        def __init__(self, formset_name_in_context, template=None, hide_labels=True):
            self.formset_name_in_context = formset_name_in_context
            self.hide_labels = hide_labels
            self.fields = []
            if template:
                self.template = template
    
        def render(self, form, form_style, context, template_pack=TEMPLATE_PACK):
            formset = context[self.formset_name_in_context]
            if self.hide_labels:
                for form in formset:
                    form.helper = FormHelper()
                    form.helper.form_show_labels = False
    
            return render_to_string(self.template, {"formset": formset})
    

    【讨论】:

      猜你喜欢
      • 2014-05-31
      • 2018-11-30
      • 2021-07-13
      • 2014-03-23
      • 2011-06-02
      • 2011-09-17
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多