Show child nodes based on the selected parent

Hi, I searched everything and can not find the answer to this question. I only have 3 months experience using python / django, so sorry my dummy question! Im using django mptt to display a simple nested set navigation.

<ul class="root"> {% recursetree nodes %} <li> {{ node.name }} {% if not node.is_leaf_node %} <ul class="children"> {{ children }} </ul> {% endif %} </li> {% endrecursetree %} 

this works fine, however I would like to show only children of the selected category (based on slug), and not all of them. Any ideas???


i finally did it like this:

 {% 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 %} 

in submissions

 category = get_object_or_404(Category, slug=slug) child = category.get_children() if not child : child = category.get_siblings() 

but it's a hack. who has an idea?

+4
source share
3 answers

You need to send some information that you are a node, and then this is a simple if .

Regarding how to send node information universally, there are several ways to do this in Django, and none of them are perfect. My preferred method is context processors: http://docs.djangoproject.com/en/1.3/ref/templates/api/#writing-your-own-context-processors

0
source
 {% 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
source

You can try the following:

 {% 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
source

All Articles