具有酥脆形式的Inlineformset-仅显示标签一次

问题描述

我有一个内联的表单集,上面显示有松脆的表单。它工作正常,但我在每行上方显示标签。我希望它们一次出现在字段表的顶部,但不太确定如何实现。

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

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 %}

解决方法

好吧,所以经过一整天的混乱之后,我终于解决了这个问题,尽管标签隐藏可能有点“ hacky”。​​

正如我提到的,可以在模板中处理标题行的显示:

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

关闭标签的操作可以在松脆的表单布局对象的定义中完成,并且需要分别在表单集中的每个表单上设置帮助程序(这是一个循环,我担心这可能有点笨拙,但是我只会这里的记录很少,所以应该不会有很大的性能开销。

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})

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...