【问题标题】:Multiple nested inheritance in templates模板中的多重嵌套继承
【发布时间】:2019-12-10 12:00:26
【问题描述】:

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

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

我可以通过单个模板中的多个 {% for %} 循环来完成,就像它描述的 here

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

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

我将展示两个模型来说明问题所在 - Parent 和 Child:

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

models.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

views.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'

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

——返回空。我想我需要将一个带有 key 的参数传递到子块中以过滤某些父级的子级,但找不到如何做到这一点的方法。

【问题讨论】:

  • 不应该是{% block content %} 而不是{% block childs_block %}
  • 哦,我错过了一些重要的事情,抱歉。父模板已更新。
  • 由于没有parent_objects_list 对象,{% for ... %} 不会运行,因此它确实不会显示任何内容。
  • 你指示它发送到哪里?请注意,模板继承与视图无关。由于一个模板可以被多个视图使用,而一个视图可以使用多个模板。
  • 抱歉没有评论预览,我需要编辑我之前的评论:但我在数据库中有一个对象(update2)。父列表显示没有问题 - 完成了。我如何在这里形成 child_objects_list?我认为这将由 ChildListView 完成,即使 ChildListView 不知道需要呈现与特定父级相关的确切对象 - 为什么它不发送所有这些对象?但首先我想知道如何根据父级动态在 {% for %} 中形成 child_objects_list,将父级参数传递给 ChildListView ?

标签: django django-templates django-views


【解决方案1】:

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

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

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

【讨论】:

  • is parent 是否可能从 {% extends %} 外部传递?我已经在上面尝试过,但 childs_block {% for %} 中仍然没有任何内容。我把它放在这里:github.com/gekmcfh/test01.git
  • 拜托,你能解释一下,parent 是从哪里来的 {% childs_block %} ?如果我理解正确,在该示例中 {% childs_block %} 只能操作从 ChildListView 收到的数据,因为在 urls.py 中已明确声明。因此 {% childs_block %} 对 parent 一无所知。要了解它,需要第二个 for 循环。所以它会是 {% for parent in parent_objects_list %} {% for child in parent.child_set.all %} 等等?
  • @gek:您在父模板中使用{% for parent in parent_objects_list %} 自己定义了parent。因此parentobject_list 中的一个元素。
  • @gek:模板引擎有一个“全局变量空间”。因此,您在父项中设置的变量会保留在子项中。
  • @gek:两者都应该有一个parent_objects_list 作为context_object_name
猜你喜欢
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多