嗨,我一直在寻找这个问题的答案,但找不到答案。我只有 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()
但这是一个黑客行为。有人有更好的主意吗?
你需要发送一些关于你所在节点的信息,然后就是一个简单的if
陈述。
关于如何通用地发送节点信息,Django中有几种方法可以做到这一点,但没有一种方法是完美的。我的首选方法是上下文处理器:http://docs.djangoproject.com/en/1.3/ref/templates/api/#writing-your-own-context-processors http://docs.djangoproject.com/en/1.3/ref/templates/api/#writing-your-own-context-processors
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)