S
S
Sergey Nizhny Novgorod2017-06-24 04:07:53
Django
Sergey Nizhny Novgorod, 2017-06-24 04:07:53

How to custom display django-mptt categories without hardcode per nesting level?

Hello.
The task is to display in such a category tree:
7ea0ba870d2740df8acd77b8ede213db.png
I.e. Levels 1 and 2 are "beautiful", then comes the standard tree.
__________
I made the usual hornbeam of everything in the view:
all_nodes = Category.objects.all()
The model itself:

class Category(MPTTModel):
    name = models.CharField(max_length=70)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

And then I made this template output:
{% for node in all_nodes %}

     {% if node.level == 0 %} 
        
        <p class="title_of_global_category">{{ node.name }}</p> #1 уровень

             {% for item in node.children.all %}

                <p class="sub_category_text">{{ item.name }}</p> №2 уровень

                    {% for child in item.children.all  %}

                        <ul class="subdeep_handler">
                            <li class="subdeep_category_text">{{ child.name }}</li> №3 уровень
                        </ul>

                    {% endfor %}
                  
            {% endfor %}
                             
     {% endif %}

{% endfor %}

those. actually pulled the next levels from each level. The problem is that I'm actually hardcoded to the nesting level. Is it possible to somehow make 3-4-5 etc. levels displayed in the tree on their own?
And what is the right way to do it all?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question