Based on django-mptt's recursetree templatetag. In addition to {{ item }} and {{ children }},
sets {{ active }} and {{ active_descendants }} in the context.
- Note that the tag takes one variable, which is a Node instance.
+ Note that the tag takes two variables: a Node instance and the key of the navigation to
+ be recursed.
Usage:
<ul>
{% recursenavigation node main %}
<li{% if active %} class='active'{% endif %}>
- {{ navigation.text }}
- {% if navigation.get_children %}
+ {{ item.text }}
+ {% if children %}
<ul>
{{ children }}
</ul>
@register.filter
def has_navigation(node, key=None):
- nav = node.navigation
- if key is not None:
- if key in nav and bool(node.navigation[key]):
- return True
- elif key not in node.navigation:
- return False
- return bool(node.navigation)
+ try:
+ nav = node.navigation
+ if key is not None:
+ if key in nav and bool(node.navigation[key]):
+ return True
+ elif key not in node.navigation:
+ return False
+ return bool(node.navigation)
+ except:
+ return False
@register.filter