Skip to content

Commit f2aa2e5

Browse files
author
yangxueguang
committed
标签云和文章归档
1 parent 651288a commit f2aa2e5

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

blog/models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
# Create your models here.
88
class ArticleManage(models.Manager):
99
def archive(self):
10-
date_list = Article.objects.datetimes('created_time', 'month')
10+
date_list = Article.objects.datetimes('created_time', 'month', order='DESC')
1111
date_dict = defaultdict(list)
1212
for d in date_list:
13-
print(type(d.year))
14-
print(type(d.month))
1513
date_dict[d.year].append(d.month)
16-
return dict(date_dict) # 模板不支持defaultdict
14+
return sorted(date_dict.items(), reverse=True) # 模板不支持defaultdict
1715

1816

1917
class Article(models.Model):

blog/templates/blog/index.html

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,16 @@ <h1 class="title"><a
4949
<!-- tags -->
5050
<h3>标签:</h3>
5151
<div class="list-group">
52-
<a href="#" class="list-group-item"><span class="badge">14</span>Cras justo odio</a>
53-
<a href="#" class="list-group-item"><span class="badge">14</span>Dapibus ac facilisis in</a>
54-
<a href="#" class="list-group-item"><span class="badge">14</span>Morbi leo risus</a>
55-
<a href="#" class="list-group-item"><span class="badge">14</span>Porta ac consectetur ac</a>
56-
<a href="#" class="list-group-item"><span class="badge">14</span>Vestibulum at eros</a>
5752
{% for tag in tag_list %}
58-
<a href="{% url 'blog:tags' tag.pk %}" class="list-group-item"><span
59-
class="badge">14</span>{{ tag.name }}</a>
53+
<a href="{% url 'blog:tag' tag.pk %}" class="list-group-item"><span
54+
class="badge"></span>{{ tag.name }}</a>
6055
{% endfor %}
6156
</div>
6257

6358
<!-- archive -->
6459
<h3>文章归档:</h3>
6560
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
66-
{% for year,months in date_archive.items %}
61+
{% for year,months in date_archive %}
6762
<div class="panel panel-default">
6863
<div class="panel-heading" role="tab" id="heading{{ forloop.counter }}">
6964
<h4 class="panel-title">
@@ -78,7 +73,7 @@ <h4 class="panel-title">
7873
aria-labelledby="heading{{ forloop.counter }}">
7974
<div class="panel-body">
8075
{% for month in months %}
81-
{{ month }} 月
76+
<p>{{ month }} 月</p>
8277
{% endfor %}
8378
</div>
8479
</div>

blog/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def get_queryset(self):
1919
def get_context_data(self, **kwargs):
2020
kwargs['category_list'] = Category.objects.all().order_by('name')
2121
kwargs['date_archive'] = Article.objects.archive()
22-
print(kwargs['date_archive'])
22+
kwargs['tag_list'] = Tag.objects.all().order_by('name')
2323
return super(IndexView, self).get_context_data(**kwargs)
2424

2525

0 commit comments

Comments
 (0)