2011-05-17 37 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

相關問題