product = category.products.all()
return render(request, 'shop/categories.html', {'categories': category, 'products': product})
categoryID = TreeForeignKey('categories', related_name='products', null=True, blank=True, db_column='categoryID', on_delete=models.CASCADE)
def show_category(request, hierarchy=None):
hierarchy = (hierarchy or "").strip("/") # Remove stray slashes
if hierarchy:
category_slug = hierarchy.split('/')
parent = None
for slug in category_slug[:-1]:
parent = Categories.objects.get(parent=parent, slug=slug)
category = Categories.objects.get(parent=parent, slug=category_slug[-1])
else:
category = None
if category:
return render(request, 'shop/categories.html', {'instance': category})
# No category, show top-level content somehow
category = Categories.objects.filter(parent=None)
return render(request, 'shop/category.html', {'categories': category})
{{ if products }}
{% for good in products.all %}
# Вывод продукта в начале, если он есть
{% endfor %}
{{ endif }}
{{ if categories }}
{% for cat in categories.get_children %}
# Вывод карточки категорий и подкатегорий, если они есть
{% endfor %}
{{ endif }}