我在生成下拉菜单时遇到问题。在管理面板中,我为每个子页面创建了“父”字段,我可以在其中定义此子页面是否是其他子页面的父页面。但是在模板子页面中,这两个地方都显示。
我试图解释:我们开始迭代。第一页没有孩子,因此显示正常。第二页也没有子级,因此显示。第三页有一个第一页的子页,因此第一页作为子页添加到树中。问题在于,由于迭代已经完成,因此不会从一开始就将其删除。
我现在不解决该问题,甚至在Django中也无法解决。我在考虑使用JS并在指定的href出现两次的情况下删除类。
{% for subpage in subpage_sorted %}
<li class="nav-item {% if subpage.parent %}dropdown{% endif %}">
<a class="nav-link {% if subpage.parent %}dropdown-toggle{% endif %}" href="{% url 'generated_page' subpage.slug %}" {% if subpage.parent %}data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"{% endif %}>{% trans subpage.title %}</a>
{% if subpage.parent %}
<div class="dropdown-menu">
<a class="dropdown-item" href="{% url 'generated_page' subpage.parent.slug %}">{% trans subpage.parent.title %}</a>
{% endif %}
</li>
{% endfor %}