django - 模板中的多重嵌套继承
问题描述
我有 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 %}
-- 返回空。我认为我需要将带有键的参数传递给子块以过滤某些父级的子级,但找不到如何做到这一点的方法。
解决方案
您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 %}
推荐阅读
- flutter - 如何使用颤振 local_notification 包显示抬头通知?
- python - 查找表以加快 Python 中的函数评估
- python - 在 Pandas 中,如何选择在一列中有重复但在另一列中有不同值的行?
- r - R:`[.default`(x, , i) 中的错误:维数不正确
- python - PySpark 到 Pandas 数据框的转换:转换时数据类型出错
- c++ - 为什么在我的简单 C++ 代码中输出是 -1717986920?
- ruby-on-rails - RSpec 测试方法是否在方法内部被调用
- r - CreateDtm 函数 R 中的 ngram_window
- javascript - 在没有 ID 或类的 div 周围插入包装器
- javascript - React Native:图像未显示