【发布时间】:2011-05-17 21:37:58
【问题描述】:
您好,我一直在寻找,但找不到答案。我只有 3 个月的使用 python/django 的经验,所以请原谅我的虚拟问题! 我使用 django mptt 来显示一个简单的嵌套集导航。
<ul class="root">
{% recursetree nodes %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
这很好用 - 但是我想只显示所选类别的子项(基于 slug)而不是全部。 有任何想法吗 ???
我终于做到了:
{% recursetree nodes %}
<li>
<a href='/{{ node.get_absolute_url}}'>{{ node.name }}</a>
</li>
{% if not node.is_leaf.node %}
{% for c in child %}
{% if c in node.get_children %}
{% if forloop.first %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endrecursetree %}
在视图中
category = get_object_or_404(Category, slug=slug)
child = category.get_children()
if not child :
child = category.get_siblings()
但这是一个黑客。有没有人有更好的主意?
【问题讨论】:
标签: django django-mptt