File tree Expand file tree Collapse file tree 2 files changed +4
-2
lines changed Expand file tree Collapse file tree 2 files changed +4
-2
lines changed Original file line number Diff line number Diff line change 1
1
from django import template
2
+ from django .db .models .aggregates import Count
2
3
3
4
from ..models import Post , Category
4
5
@@ -17,4 +18,5 @@ def archives():
17
18
18
19
@register .simple_tag
19
20
def get_categories ():
20
- return Category .objects .all ()
21
+ # 记得在顶部引入 count 函数
22
+ return Category .objects .annotate (num_posts = Count ('post' )).filter (num_posts__gt = 0 )
Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ <h3 class="widget-title">分类</h3>
110
110
{% for category in category_list %}
111
111
< li >
112
112
< a href ="{% url 'blog:category' category.pk %} "> {{ category.name }} < span
113
- class ="post-count "> (13 )</ span > </ a >
113
+ class ="post-count "> ({{ category.num_posts }} )</ span > </ a >
114
114
</ li >
115
115
{% empty %}
116
116
暂无分类!
You can’t perform that action at this time.
0 commit comments