Search code examples
pythondjangodrop-down-menudjango-cmsnodechildren

get_children not working. get_descendants does. But i can't use that


I am currently working on the navbar of a project with Django-cms.
I am fairly new to this framework and language, so sorry if this is a stupid question.
This has double dropdowns, which respond to user changes in the Django-cms admin interface. Which works. Sort of.


The problem is that get_children doesn't work (no errors or something, just not detecting children, and showing the 'should be dropdown button' as a non dropdown version), but get_descendants does.
But if i use that the content of the second dropdown will be shown again in the first dropdown.
So get_children will be perfect, as it will only show the direct descendants, instead of all.

    {% load cms_tags menu_tags sekizai_tags staticfiles%}
{% load menu_tags %}
{% for child in children %}

        <!--non dropdown 1-->
        {% if child.is_leaf_node %}
            <li><a href="{{ child.get_absolute_url }}">{{child.get_menu_title }}</a></li>
        {% endif %}

        <!--dropdown 1-->
        {% if not child.is_leaf_node or child.ancestor %}
        <div class="dropdown">
            <li><a href="{{ child.get_absolute_url }}" class="dropbtn">{{child.get_menu_title }}<b class="caret"></b></a></li>

            <!-- dropdown 1 content-->
            {% if child.get_descendants %}
                <div class="dropdown-content">
                {% for kid in child.get_descendants %}

                    <!--non dropdown 2-->
                    {% if kid.is_leaf_node %}
                    <li><a href="{{ kid.get_absolute_url }}">{{kid.get_menu_title }}</a></li>
                    {% endif %}

                        <!--dropdown 2 -->
                        {% if not child.is_leaf_node or child.ancestor %}
                        <li>
                            <a class="menu-has-sub">{{kid.get_menu_title }}<i class="fa fa-angle-down"></i></a>
                                <!-- dropdown 2 content-->
                                <ul class="sub-dropdown droppeddown">
                                    {% for baby in kid.get_descendants %} 
                                        <li><a href="{{ baby.get_absolute_url }}">{{baby.get_menu_title }}</a></li>
                                    {% endfor %}
                                </ul>
                            </li>   
                        {% endif %}    

                {% endfor %}
            </div>
                {% endif %}
          </div>

        {% endif %}

{% endfor %}

So my question is: Why can't i use children

EDIT: *Why can't i use get_children. As in the function.
No child labour here.


Solution

  • Nvm i fixed it!

    The syntax in this case should be children instead of get_children. which is funny because of that edit above.

    Anyway, here's an example:

    This doesn't work:
    {% for kid in child.get_children %}
    This does work:
    {% for kid in child.children%}

    hopefully this will help anyone else having this little struggle.