在Django模板中获取当前子类别MPTT

时间:2019-11-30 11:06:51

标签: django django-mptt

我的观点:

def categorylist(request):
    categorynodes = Category.objects.add_related_count(Category.objects.all(), Categories, 'category')
    context = {
        'categorynodes': categorynodes,
    }
    return render(request, 'categorylist.html', context)

def subcategory(request):
    categorynodes = Category.objects.add_related_count(Category.objects.filter(level__lte=1), Categories, 'category')
    context = {
        'categorynodes': categorynodes,
    }
    return render(request, "subcategory.html", context)

我有categorylist.html模板:

{% recursetree categorynodes %}
    <ul>
        {% if node.is_root_node %}
        <a href="{{ node.slug }}">{{ node.title }}</a>
        {% elif node.is_child_node and not node.is_leaf_node or node.is_second_node %}
        <a href="{{ node.slug }}">{{ node.title }}</a>
        {% endif %}

        {% if not node.is_leaf_node %}
        <li>
            {{ children }}
        </li>
        {% endif %}
    </ul>
{% endrecursetree %}

我有subcategory.html模板:

{% recursetree categorynodes %}

{% if node.is_root_node %}
<h1>{{ node.title }}</h1>
{% endif %}

{% endrecursetree %}

类别树正确显示:

  • 类别1
    • 子类别1.1
    • 子类别1.2
  • 类别2
    • 子类别2.1
    • 子类别2.2

当我从categorylist.html访问Sub Category2.2的链接时,如何打印标题“ Sub Category2.2”?

现在,它为所有子类别显示相同的“子类别1.1”。

0 个答案:

没有答案