[Fixed]-DjangoCMS NavigationNode – custom node

1πŸ‘

βœ…

I finally found the solution. The answer is: Yes! It is possible.

As it states in the documentation, you can use modify() method.

But it did not work for me, because I got confused with node.attr["changed_by"] . In templates, I wanted to use something like this: {{ child.category_name }} but as it is obvious, I was modifying it wrong.

The correct way is this:

from menus.base import Modifier
from menus.menu_pool import menu_pool

from cms.models import Page

class MyMode(Modifier):
    """

    """
    def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
        # if the menu is not yet cut, don't do anything
        if post_cut:
            return nodes
        # otherwise loop over the nodes
        for node in nodes:
            # does this node represent a Page?
            if node.attr["is_page"]:
                # if so, put its changed_by attribute on the node
                node.category_name = "Some category name here"
        return nodes

menu_pool.register_modifier(MyMode)

Now, in menu.html, you can use child.category_name and it will output the string β€œSome category name here”

{% load i18n menu_tags cache %}

{% for child in children %}
<li>
    {% if child.children %}
    <a href="{{ child.get_absolute_url }}">
        {{ child.get_menu_title }} <span class="caret"></span>
    </a>
    <ul class="menu-vertical">
        {% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
    </ul>
    {% else %}
        <a class1="{{ child.get_absolute_url }}" href="{{ child.get_absolute_url }}">
            {% if child.category_name %}
                <b>{{ child.category }}</b>
            {% endif %}
            {{ child.get_menu_title }}
        </a>
    {% endif %}
</li>
{% if class and forloop.last and not forloop.parentloop %}{% endif %}
{% endfor %}

Finally, after hours and hours of attempts, I solved this.

πŸ‘€Peter

Leave a comment