2011-05-17 23 views
4

嗨,我一直在找遍,找不到答案。我只有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 %} 

这工作得很好 - 但我想只显示所选类别(基于蛞蝓),而不是所有的人的孩子。 任何想法???


我终于做到了这样:

{% 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() 

,但它是一个黑客。有没有人有更好的主意?

回答

0
{% recursetree nodes %} 
    <li> 
     <a href="/category/{{ node.get_absolute_url }}">{{ node.name }}</a>       
     {% if node.name == category.name %} 
     <ul> 
      {{ children }} 
     </ul> 
     {% endif %} 
    <li> 
{% endrecursetree %} 
0

你可以试试这个:

{% recursetree nodes %} 
    #check if the node is a child node 
    {% if node.is_child_node %} 
     <a href="{{ node.get_absolute_url }}" >{{ node.name }}</a> 
    {% endif %} 

    #check if a root node is the current category 
    {% if node.is_root_node and node.name == category.name %} 
     {{ children }} 
    {% endif %} 

{% endrecursetree %} 
+0

如果您提供的代码,然后请说明*为什么*此代码的解决问题。 *“你可以试试这个:”*是不够的。请相应地编辑你的答案。 – 2015-04-04 23:32:48

相关问题