Django 管理员:自定义内联模板 (tabular.html)

发布于 2024-08-20 09:12:37 字数 3611 浏览 2 评论 0原文

我正在尝试遵循 这个答案,但我对如何编辑模板感到困惑。

我的admin.py的相关部分:

SegmentFormset = forms.models.inlineformset_factory(Division,Segment)
class DivisionForm(forms.ModelForm):
    def __init__(self, **kwargs):
        super(DivisionForm, self).__init__(**kwargs)
        self.segment_formset = SegmentFormset(instance=self.instance, data=self.data,
                prefix=self.prefix)

    def is_valid(self):
        return (super(DivisionForm, self).is_valid() and 
                    self.segment_formset.is_valid())

    def save(self, commit=True):
        assert commit == True 
        res = super(DivisionForm, self).save(commit=commit)
        self.segment_formset.save()
        return res

class DivisionInline(admin.TabularInline):
    model = Division
    form = DivisionForm
    template = 'competitions/admin/tabular.html'

class CompetitionAdmin(VersionAdmin):
    inlines = [DivisionInline,]

我的模板的相关部分:

    {% for fieldset in inline_admin_form %}
      {% for line in fieldset %}
        {% for field in line %}
          <td class="{{ field.field.name }}">
          {{ field.field.errors.as_ul }}
          {{ field.field }}
          </td>
        {% endfor %}
      {% endfor %}
         <td>My segment formset should be here</td>
    {% endfor %}

我不知道如何访问分段表单集。我已经尝试了所有变量名称,但没有一个是我的 DivisionForm。除法表单集是 {{fieldset.formset}} 变量,这是我所能得到的。

编辑1:

实际上,模板的相关部分有点长;)放入更多代码:

 <tbody>
 {% for inline_admin_form in inline_admin_formset %}
    {% if inline_admin_form.form.non_field_errors %}
    <tr><td colspan="{{ inline_admin_form.field_count }}">{{ inline_admin_form.form.non_field_errors }}</td></tr>
    {% endif %}
    <tr class="{% cycle row1,row2 %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}">

    <td class="original">
      {% if inline_admin_form.original or inline_admin_form.show_url %}<p>
      {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
      {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
        </p>{% endif %}
      {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
      {{ inline_admin_form.fk_field.field }}
      {% spaceless %}
      {% for fieldset in inline_admin_form %}
        {% for line in fieldset %}
          {% for field in line %}
            {% if field.is_hidden %} {{ field.field }} {% endif %}
          {% endfor %}
        {% endfor %}
      {% endfor %}
      {% endspaceless %}
    </td>

    {% for fieldset in inline_admin_form %}
      {% for line in fieldset.formset %}
        {% for field in line %}
          <td class="{{ field.field.name }}">
          {{ field.field.errors.as_ul }}
          {{ field.field }}
          </td>
        {% endfor %}
      {% endfor %}
      <td>My segment formset should be here</td>
    {% endfor %}

    {% if inline_admin_formset.formset.can_delete %}
      <td class="delete">{% if inline_admin_form.original %}{{ inline_admin_form.deletion_field.field }}{% endif %}</td>
    {% endif %}

    </tr>

 {% endfor %}
 </tbody>

I'm trying to follow the guidelines in this answer, but I'm getting stuck with how to edit the template.

The relevant part of my admin.py:

SegmentFormset = forms.models.inlineformset_factory(Division,Segment)
class DivisionForm(forms.ModelForm):
    def __init__(self, **kwargs):
        super(DivisionForm, self).__init__(**kwargs)
        self.segment_formset = SegmentFormset(instance=self.instance, data=self.data,
                prefix=self.prefix)

    def is_valid(self):
        return (super(DivisionForm, self).is_valid() and 
                    self.segment_formset.is_valid())

    def save(self, commit=True):
        assert commit == True 
        res = super(DivisionForm, self).save(commit=commit)
        self.segment_formset.save()
        return res

class DivisionInline(admin.TabularInline):
    model = Division
    form = DivisionForm
    template = 'competitions/admin/tabular.html'

class CompetitionAdmin(VersionAdmin):
    inlines = [DivisionInline,]

The relevant part of my template:

    {% for fieldset in inline_admin_form %}
      {% for line in fieldset %}
        {% for field in line %}
          <td class="{{ field.field.name }}">
          {{ field.field.errors.as_ul }}
          {{ field.field }}
          </td>
        {% endfor %}
      {% endfor %}
         <td>My segment formset should be here</td>
    {% endfor %}

What I can't figure out is how to access the segment formset. I've experimented with all of the variable names and none of them are my DivisionForm. The division formset is the {{fieldset.formset}} variable and that's as far as I've been able to get.

Edit 1:

Actually, the relevant part of the template is a bit longer ;) Putting in more code:

 <tbody>
 {% for inline_admin_form in inline_admin_formset %}
    {% if inline_admin_form.form.non_field_errors %}
    <tr><td colspan="{{ inline_admin_form.field_count }}">{{ inline_admin_form.form.non_field_errors }}</td></tr>
    {% endif %}
    <tr class="{% cycle row1,row2 %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}">

    <td class="original">
      {% if inline_admin_form.original or inline_admin_form.show_url %}<p>
      {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
      {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
        </p>{% endif %}
      {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
      {{ inline_admin_form.fk_field.field }}
      {% spaceless %}
      {% for fieldset in inline_admin_form %}
        {% for line in fieldset %}
          {% for field in line %}
            {% if field.is_hidden %} {{ field.field }} {% endif %}
          {% endfor %}
        {% endfor %}
      {% endfor %}
      {% endspaceless %}
    </td>

    {% for fieldset in inline_admin_form %}
      {% for line in fieldset.formset %}
        {% for field in line %}
          <td class="{{ field.field.name }}">
          {{ field.field.errors.as_ul }}
          {{ field.field }}
          </td>
        {% endfor %}
      {% endfor %}
      <td>My segment formset should be here</td>
    {% endfor %}

    {% if inline_admin_formset.formset.can_delete %}
      <td class="delete">{% if inline_admin_form.original %}{{ inline_admin_form.deletion_field.field }}{% endif %}</td>
    {% endif %}

    </tr>

 {% endfor %}
 </tbody>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花随流水 2024-08-27 09:12:37

表单集就像列表,所以你可以像这样递归它

{% for form in fieldset.formset %}
{{ form.as_p }}
{% endfor %}

Formsets are like lists, so you can recurse it like

{% for form in fieldset.formset %}
{{ form.as_p }}
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文