Collapse tree structure in django

I have a tree structure in Django. It is displayed as a menu. To make it compact, you need to design it as an expandable/collapsible list. How to do this?

class Composition(MPTTModel):

parent = TreeForeignKey('self', on_delete=models.CASCADE, blank=True, null=True, related_name="children", db_index=True)
mapping = models.BooleanField(default=False)
id_drawing = models.ImageField(upload_to='media/image/', blank=True, null=True, default="null")
position = models.PositiveIntegerField()
designstion = models.CharField(max_length=255, primary_key=True)
name = models.CharField(max_length=255, default="null")
description = models.TextField(blank=True, null=True, default="null")
quantities = models.PositiveIntegerField(blank=True, null=True, default="null")
slug = models.SlugField()

objects = TreeManager()
def drawing_url(self):
    if self.id_drawing and hasattr(self.id_drawing, 'url'):
        return self.id_drawing.url

class MPTTMeta:
    order_insertion_by = ['designstion']

class Meta:

def __str__(self):
    return "%s (%s)" % (, self.designstion)

    {% recursetree catalog_list %}
        <li >
            <a href="{{ node.get_absolute_url }}">{{ }}</a>
            {% if not node.is_leaf_node %}
                <ul class="children">
                    {{ children }}
            {% endif %}
    {% endrecursetree %}

So far it outputs like this:

enter image description here

It should be like this:
enter image description here

Leave a Comment