首页 > 解决方案 > 模板中的多重嵌套继承

问题描述

我有 4 个模型需要通过链式继承来呈现。像那样:

├───GRANDPARENT1
│   ├───PARENT11
│   │   ├───CHILD111
│   │   └───CHILD112
│   └───PARENT12
│       ├───CHILD121
│       └───CHILD122
└───GRANDPARENT2
    ├───PARENT21
    │   ├───CHILD211
    │   └───CHILD212
    └───PARENT22
        ├───CHILD221
        └───CHILD222

我可以通过单个模板中的多个 {% for %} 循环来做到这一点,就像这里描述的那样

{% for grandparent in grandparents %}
    <p>{{ grandparent.title }}'s childs is:</p>
    {% for parent in grandparent.parent_set.all %}
        <p>{{ parent.title }}'s childs is:</p>
        {% for child in parent.child_set.all %}
             <p>{{ parent.title }}</p>
        {% endfor %}
    {% endfor %}
{% endfor %}

但我想将每个类划分到它自己的模板上,并将它们带到相同的形式中,以便于随后添加额外的嵌套级别。

我将展示两个模型来说明问题所在 - 父母和孩子:

我尝试从 base.html 扩展父模板 - 它有效。但是接下来,当我从 parent.html 扩展 child.html 模板时,它什么也不输出。

模型.py:

class Parent(models.Model):
    title = models.CharField(
        max_length=150,
    )
class Child(models.Model):
    title = models.CharField(
        max_length=150,
    )
    parents = models.ManyToManyField(
        Parent,
        blank=True
    )

更新 2

在数据库中,我有两个父类对象(PARENT1、PARENT2)和四个子类对象(CHILD11、CHILD12、CHILD21、CHILD22)。CHILD11 和 CHILD 12 在父母多对多关系中有 PARENT1;CHILD21 和 CHILD22 在 PARENT2 上具有相同的结构,因此具有以下结构:

├───PARENT1
│   ├───CHILD11
│   └───CHILD12
└───PARENT2
    ├───CHILD21
    └───CHILD22

视图.py:

class ParentListView(ListView):
    model = Parent
    template_name = 'parent_list.html'
    context_object_name = 'parent_objects_list'


class ChildListView(ListView):
    model = Child
    template_name = 'child_list.html'
    context_object_name = 'child_objects_list'

网址.py

from django.urls import path

from .views import (
    ParentListView,
    ChildListView
)

urlpatterns = [
    path('parents/', ParentListView.as_view(), name='parents'),
    path('childs/', ChildListView.as_view(), name='childs'),
]

parent_list.html(更新):

{% extends '_base.html' %}

{% block title %}Parents_list{% endblock title %}

{% block content %}
    {% for parent in parent_objects_list %}
        <div class="card">
            <div class="card-header">
                <span class="font-weight-bold">{{ parent.title }}</span>
            </div>
            <div class="card-body">
                {% block childs_block %}
                {% endblock childs_block %}
            </div>
        </div>
    {% endfor %}
{% endblock content %}

child_list.html

{% extends 'parent_list.html' %}

{% block title %}Childs_list{% endblock title %}

{% block childs_block %}
    {% for child in child_objects_list %}
        <div class="card">
            <div class="card-header">
                <span class="font-weight-bold">{{ child.title }}</span>
            </div>
        </div>
    {% endfor %}
{% endblock childs_block %}

-- 返回空。我认为我需要将带有键的参数传递给子块以过滤某些父级的子级,但找不到如何做到这一点的方法。

标签: djangodjango-templatesdjango-views

解决方案


ChildListView只发送一个查询child_objects_list。因此,这意味着parent_objects_list您渲染模板时没有,因此{% for ... %}循环不会循环。

因此,您可以在 中传递Parent对象列表ChildListView,最好使用模型prefetch_related上的a child

class ChildListView(ListView):
    model = Parent
    template_name = 'child_list.html'
    context_object_name = 'parent_objects_list'
    queryset = Parent.objects.prefetch_related('child')

然后,您可以遍历child_set

{% extends 'parent_list.html' %}

{% block title %}Childs_list{% endblock title %}

{% block childs_block %}
    {% for child in parent.child_set.all %}
        <div class="card">
            <div class="card-header">
                <span class="font-weight-bold">{{ child.title }}</span>
            </div>
        </div>
    {% endfor %}
{% endblock childs_block %}

推荐阅读