B
B
blackbb2018-10-31 11:45:23
Django
blackbb, 2018-10-31 11:45:23

How to add a class to the active menu item in django mtpp?

My menu code

<ul class="header_ul navbar-nav mt-2 mt-lg-0 margin-auto">
                    {% recursetree menu_top %}
                    <li class="nav-item{% if not node.is_leaf_node %}dropdown{% endif %}">
                        <a {% if node.slug in request.path %} class="current" {% endif %} {% if not node.is_leaf_node %}class="dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"{% else %} href="/{{node.slug}}"{% endif %}>{% if node.icon %}<i class="fa {{node.icon}}"></i> {% else %}{% endif %}{{node.title}}</a>
                        {% if not node.is_leaf_node %}
                        <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                          {{children}}
                        </ul>
                        {% endif %}
                    </li>
                    {% endrecursetree %}
                  </ul>

This code works, but there are requests in the filter such that the request.path includes the slug of some items, and, in fact, they become active. For example, such a request /handbooks/viewofcargo/passazhirskie/ includes two menu items (cargo and handbook) and there are many such requests. How to exclude such moments?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Segey K., 2018-10-31
@blackbb

As an option, write a template filter that will wrap node.slug with slashes

@register.filter
def to_path(value):
    return '/' + value + '/'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question