Last active
September 4, 2017 19:19
-
-
Save LowerDeez/e16aa24b82daa87c1b2b2831faea4c94 to your computer and use it in GitHub Desktop.
Django. Django-mptt. Displaying checkboxes for categories, which have articles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- With bootstrap accordeon for category's children --> | |
<ul class="categories"> | |
<li> | |
<form id="all_categories_form" action="{{ url('articles:home') }}" method="get"> | |
<input type="hidden" name="is_clear" value="True"> | |
<a href="#" onclick="document.getElementById('all_categories_form').submit();">All</a> | |
</form> | |
</li> | |
{% cache 150 "categories_tree" %} | |
<form action="" method="get" class="form-group"> | |
{% for node in categories recursive %} | |
{% set node_children = node.get_children() %} | |
{% if node.has_articles %} {# using to hide child categories, which have no articles #} | |
<li> | |
{% if node_children %} | |
<a class="category-dropdown" id="{{ node.id }}" type="button" | |
data-toggle="collapse" data-target="#category-{{ node.id }}"> | |
<span class="glyphicon glyphicon-chevron-right"></span> | |
</a> | |
{% endif %} | |
<input class="category-checkbox" id="category-check-{{ node.id }}" | |
type="checkbox" name="categories" value="{{ node.id }}"> | |
<a | |
{% if category %} | |
{% if node.slug == category.slug %} | |
class="selected" | |
{% endif %} | |
{% endif %} href="{{ node.get_absolute_url() }}" > | |
{{ node.name }} | |
</a> | |
</li> | |
{% endif %} | |
{% if node_children %} | |
<div id="category-{{ node.id }}" class="collapse"> | |
<ul class="children child-categories"> | |
{{ loop(node_children) }} | |
</ul> | |
</div> | |
{% endif %} | |
{% endfor %} | |
<button class="btn btn-success btn-category-search" type="submit">Find several categories</button> | |
</form> | |
{% endcache %} | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Query | |
categories = Category.objects.exclude(articles__isnull=True).select_related('parent')\ | |
.prefetch_related('translations').distinct() # get_cached_trees returns only root nodes, so children, which have articles, | |
# but their parents are not, displaying not correctly | |
# get checked categories and filter categories by them | |
filter_cat_ids = self.request.GET.getlist('categories') | |
if filter_cat_ids: | |
filter_categories = Category.objects.filter(id__in=filter_cat_ids).prefetch_related('translations') | |
if filter_categories: | |
articles = Article.objects.get_published()\ | |
.filter(categories__in=filter_categories.get_descendants(include_self=True))\ | |
.prefetch_related('translations', 'categories', 'images').distinct() | |
context['articles'] = articles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment