Question

I've created a page and app-hook for my custom application and now I need to know how to integrate this application with default breadcrumbs. All I've found is Navigation Modifiers in official documentation. But those examples are not descriptive enough for me, I don't know how to use them in my case. Let's say I have models like these:

class Category(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=30)

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField(max_length=1000)
    category = models.ForeignKey(Category)

And I use urls like /category.slug/ to show all posts in category and /category.slug/post.id to show post's content.

Était-ce utile?

La solution

Use Navigation Modifiers like this.

In the myapp/menu.py:

from menus.base import NavigationNode
from menus.menu_pool import menu_pool
from cms.menu_bases import CMSAttachMenu
from models import Category, Post

class CategoryMenu(CMSAttachMenu):

    name = ("Category Menu")

    def get_nodes(self, request):
        nodes = []

        for category in Category.objects.all():
            node = NavigationNode(
                category.title,
                category.get_absolute_url(),
                category.pk,
            )
            nodes.append(node)
            for post in Post.objects.filter(category=category):
                node2 = NavigationNode(
                    post.title,
                    post.get_absolute_url(),
                    post.pk,
                    category.pk
                )
                nodes.append(node2)
        return nodes

menu_pool.register_menu(CategoryMenu)

Now you can select the menu for page you hooked the app and display the breadcrumbs in the template.

Oh and you have to add get_absolute_url to the models: https://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url

Autres conseils

As django-cms documentation say:

If the current URL is not handled by the CMS or you are working in a navigation extender, you may need to provide your own breadcrumb via the template. This is mostly needed for pages like login, logout and third-party apps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top