@WebDev921

Как в django-mptt вывести только первых двух потомков?

Есть такой шаблон:
{% recursetree product.comments.all %}
               {% with comment=node %}
                    <div class="comment-item_lvl-{{ comment.level|add:'1' }}">
                        {% include 'product/comment.html' %}
                    </div>
                {% endwith %}
                {% if not node.is_leaf_node %}
                      {{ children }}
                {% endif %}
 {% endrecursetree %}


Как задать вывод только первых 2 потомков а не всех?
  • Вопрос задан
  • 259 просмотров
Решения вопроса 1
Level опытным путем сами определите
{% recursetree product.comments.all %}
    {% with comment=node %}
    <div class="comment-item_lvl-{{ comment.level|add:'1' }}">
        {% include 'product/comment.html' %}
    </div>
    {% endwith %}
    {% if not node.is_leaf_node and node.level <= 2 %}
        {{ children }}
    {% endif %}
{% endrecursetree %}


И еще в догонку, так лучше
{% recursetree product.comments.all %}
    <div class="comment-item_lvl-{{ node.level|add:'1' }}">
        {% include 'product/comment.html' with comment=node %}
    </div>
    {% if not node.is_leaf_node and node.level <= 2 %}
        {{ children }}
    {% endif %}
{% endrecursetree %}
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
alternativshik
@alternativshik
{% recursetree product.comments.all | slice:":2" %}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы