[英]How to show a certain number of levels in django-mptt?
In my Django project I use django-mptt application to create hierarchical tree. 在我的Django项目中,我使用django-mptt应用程序创建分层树。 Right now next code works well but I want to show only first 4 level of the tree.
现在,下一个代码运行良好,但是我只想显示树的前4个级别。 How to make it correctly?
如何正确制作? I am confused.
我很困惑。
views.py: views.py:
context['caregories'] = Category.objects.get(id=5).get_descendants()
html: HTML:
{% load mptt_tags %}
<ul>
{% recursetree caregories %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
You can filter the descendants by their level 您可以按等级过滤后代
obj = Category.objects.get(id=5)
context['caregories'] = obj.get_descendants().filter(level__lte=obj.level + max_depth)
where max_depth
is the depth you require 其中
max_depth
是您需要的深度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.