Answer the question
In order to leave comments, you need to log in
Django mptt getting parents?
Good afternoon! I created a tree menu using mptt, but when implementing bread cats, I came across a problem: I can’t display all parents. Actually the code itself:
class CategoryView(views.IndexView):
"""
View: Страница категории
"""
def get_context_data(self, **kwargs):
category = get_object_or_404(Category, slug=self.kwargs['slug'], id=self.kwargs['id'])
products = Product.objects.filter(category__slug=self.kwargs['slug'])
metatags = create_metatags(category.name)
return {
'products': products,
'category': category,
'breadcrumb': get_breadcrumb(category),
'metatags': metatags,
'section': 'menu/product_category.html'
}
....
def get_breadcrumb(category):
breadcrumb = []
for parent in category.get_ancestors(ascending=True, include_self=True):
breadcrumb.append({
'url': parent.get_absolute_url(),
'title': parent.name
})
return breadcrumb
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question