我正在使用django-mptt
库作为该类别。我可以在模板中显示类别列表,但我想缩进它,以便用户可以知道哪个是主要类别,哪个是子类别等。我试图构建的方式是
{% recursetree nodes %}
<li class="node">
<a href="/category/{{ node.get_absolute_url }}"
class="{% if not node.is_leaf_node and not node.is_root_node and node.is_child_node %} child_parent {% elif node.is_leaf_node and not node.is_root_node %}leaf_node{% endif %}">
{{ node.name }}
{% if node.is_root_node %}
<span class="badge">{{ node.get_count_children }}</span>
{% endif %}
</a>
{% if not node.is_leaf_node %}
<ul class="children">
<li>{{ children }}</li>
</ul>
{% endif %}
</li>
{% endrecursetree %}
这产生了以下类别
的设计这里的梳妆台是卧室用品的孩子,如床和Almirah不是床的孩子。我怎么能解决这个问题?我知道问题在这里
<a href="/category/{{ node.get_absolute_url }}"
class="{% if not node.is_leaf_node and not node.is_root_node and node.is_child_node %} child_parent {% elif node.is_leaf_node and not node.is_root_node %}leaf_node{% endif %}">
但无法解决此问题
不是截图中的梳妆台
答案 0 :(得分:2)
根据你更新的答案。
Dinning Set, Kitchen Rack, and Kitchen Setup(Modular Kitchen)
应该是cyan
,因为它们是第二级。
如果我的理解是正确的。
这是我的黑客解决方案。如果有人发现更好的请加注。
Model
实例nodes
添加到context
。 (如果你像我一样使用Django2.0,这将是一个可选项) models.py
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
class Genre(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True,
on_delete=models.CASCADE)
class MPTTMeta:
order_insertion_by = ['name']
def __str__(self):
return f'{self.name}'
def is_second_node(self):
return True if (self.get_ancestors().count() == 1) else False
views.py
from django.views.generic import ListView
from genres.models import Genre
class GenreListView(ListView):
model = Genre
template_name = 'genre_list.html'
def get_context_data(self, *, object_list=None, **kwargs):
"""Get the context for this view."""
queryset = object_list if object_list is not None else self.object_list
page_size = self.get_paginate_by(queryset)
context_object_name = self.get_context_object_name(queryset)
if page_size:
paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
context = {
'paginator': paginator,
'page_obj': page,
'is_paginated': is_paginated,
'object_list': queryset
}
else:
context = {
'paginator': None,
'page_obj': None,
'is_paginated': False,
'object_list': queryset
}
if context_object_name is not None:
context[context_object_name] = queryset
context.update(kwargs)
context['nodes'] = context.get('object_list')
return super().get_context_data(**context)
genre_list.html
<!DOCTYPE html>
{% load mptt_tags %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Genre ListView</title>
</head>
<style>
.root {color: purple}
.child{color: cyan}
.leaf {color: gray}
</style>
<body>
{% recursetree nodes %}
<div class="
{% if node.is_root_node %}
root
{% elif node.is_child_node and not node.is_leaf_node or node.is_second_node%}
child
{% elif node.is_leaf_node and not node.is_root_node%}
leaf
{%endif%}">
{{node.name}}
{{node.is_second_node}}
</div>
{% if not node.is_leaf_node%}
<ul>{{children}}</ul>
{% endif %}
{% endrecursetree %}
</ul>
</body>
</html>