Skip to content

Commit 651288a

Browse files
author
yangxueguang
committed
New - 文章归档
1 parent 43fd57b commit 651288a

File tree

4 files changed

+36
-50
lines changed

4 files changed

+36
-50
lines changed

blog/admin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from django.contrib import admin
22

33
# Register your models here.
4-
from blog.models import Article, Category
4+
from blog.models import Article, Category, Tag
55

66
admin.site.register(Article)
7-
admin.site.register(Category)
7+
admin.site.register(Category)
8+
admin.site.register(Tag)

blog/models.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
from django.db import models
22
from django.core.urlresolvers import reverse
3+
from collections import defaultdict
4+
import datetime
35

46

57
# Create your models here.
68
class ArticleManage(models.Manager):
79
def archive(self):
8-
date_list = Article.objects.dates('created_time', 'mouth')
10+
date_list = Article.objects.datetimes('created_time', 'month')
11+
date_dict = defaultdict(list)
12+
for d in date_list:
13+
print(type(d.year))
14+
print(type(d.month))
15+
date_dict[d.year].append(d.month)
16+
return dict(date_dict) # 模板不支持defaultdict
917

1018

1119
class Article(models.Model):
@@ -14,10 +22,12 @@ class Article(models.Model):
1422
('p', 'Published'),
1523
)
1624

25+
objects = ArticleManage()
26+
1727
title = models.CharField('标题', max_length=70)
1828
body = models.TextField('正文')
19-
created_time = models.DateTimeField('创建时间', auto_now_add=True)
20-
last_modified_time = models.DateTimeField('修改时间', auto_now=True)
29+
created_time = models.DateTimeField('创建时间')
30+
last_modified_time = models.DateTimeField('修改时间')
2131
status = models.CharField('文章状态', max_length=1, choices=STATUS_CHOICES)
2232
abstract = models.CharField('摘要', max_length=54, blank=True, null=True, help_text="可选,如若为空将摘取正文的前54个字符")
2333
views = models.PositiveIntegerField('浏览量', default=0)

blog/templates/blog/index.html

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -63,54 +63,27 @@ <h3>标签:</h3>
6363
<!-- archive -->
6464
<h3>文章归档:</h3>
6565
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
66-
<div class="panel panel-default">
67-
<div class="panel-heading" role="tab" id="headingOne">
68-
<h4 class="panel-title">
69-
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"
70-
aria-expanded="true" aria-controls="collapseOne">
71-
2001
72-
</a>
73-
</h4>
74-
</div>
75-
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel"
76-
aria-labelledby="headingOne">
77-
<div class="panel-body">
78-
nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
79-
</div>
80-
</div>
81-
</div>
82-
<div class="panel panel-default">
83-
<div class="panel-heading" role="tab" id="headingTwo">
84-
<h4 class="panel-title">
85-
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion"
86-
href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
87-
2002
88-
</a>
89-
</h4>
90-
</div>
91-
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel"
92-
aria-labelledby="headingTwo">
93-
<div class="panel-body">
94-
nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
66+
{% for year,months in date_archive.items %}
67+
<div class="panel panel-default">
68+
<div class="panel-heading" role="tab" id="heading{{ forloop.counter }}">
69+
<h4 class="panel-title">
70+
<a role="button" data-toggle="collapse" data-parent="#accordion"
71+
href="#collapse{{ forloop.counter }}"
72+
aria-expanded="true" aria-controls="collapse{{ forloop.counter }}">
73+
{{ year }} 年
74+
</a>
75+
</h4>
9576
</div>
96-
</div>
97-
</div>
98-
<div class="panel panel-default">
99-
<div class="panel-heading" role="tab" id="headingThree">
100-
<h4 class="panel-title">
101-
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion"
102-
href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
103-
2003
104-
</a>
105-
</h4>
106-
</div>
107-
<div id="collapseThree" class="panel-collapse collapse" role="tabpanel"
108-
aria-labelledby="headingThree">
109-
<div class="panel-body">
110-
nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
77+
<div id="collapse{{ forloop.counter }}" class="panel-collapse collapse in" role="tabpanel"
78+
aria-labelledby="heading{{ forloop.counter }}">
79+
<div class="panel-body">
80+
{% for month in months %}
81+
{{ month }} 月
82+
{% endfor %}
83+
</div>
11184
</div>
11285
</div>
113-
</div>
86+
{% endfor %}
11487
</div>
11588
</div>
11689
</div>

blog/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def get_queryset(self):
1818

1919
def get_context_data(self, **kwargs):
2020
kwargs['category_list'] = Category.objects.all().order_by('name')
21+
kwargs['date_archive'] = Article.objects.archive()
22+
print(kwargs['date_archive'])
2123
return super(IndexView, self).get_context_data(**kwargs)
2224

2325

0 commit comments

Comments
 (0)