TodayArchiveView, YearArchiveView, MonthArchiveView, DayArchiveView这类视图都是用来显示数据,用法也差不多,重点在于URL而已:
TodayArchiveView:搜索今天的数据
YearArchiveView:搜索指定年份的数据
MonthArchiveView:搜索指定月份的数据
DayArchiveView:搜索指定日的数据
建一张表来练习练习,用于存放检查数据库备份情况的Backup表
models.py
1
2
3
4
5
6
7
8
9
|
class
Backup(models.Model):
server
=
models.ForeignKey(ServerList)
checkdate
=
models.DateField()
admin_statistics_status
=
models.IntegerField()
game_status
=
models.IntegerField()
gm_status
=
models.IntegerField()
media_status
=
models.IntegerField()
operation_status
=
models.IntegerField()
runtime
=
models.DateTimeField()
|
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
class
BackupTodayArchive(TodayArchiveView):
allow_empty
=
True
allow_future
=
False
context_object_name
=
'list'
template_name
=
'backuplist.jinja.html'
http_method_names
=
[u
'get'
,]
model
=
Backup
date_field
=
'checkdate'
paginate_by
=
50
make_object_list
=
True
class
BackupYearArchive(YearArchiveView):
allow_empty
=
True
allow_future
=
False
context_object_name
=
'list'
template_name
=
'backuplist.jinja.html'
http_method_names
=
[u
'get'
,]
model
=
Backup
date_field
=
'checkdate'
year_format
=
'%Y'
paginate_by
=
50
make_object_list
=
True
class
BackupMonthArchive(MonthArchiveView):
allow_empty
=
True
allow_future
=
False
context_object_name
=
'list'
template_name
=
'backuplist.jinja.html'
http_method_names
=
[u
'get'
,]
model
=
Backup
date_field
=
'checkdate'
year_format
=
'%Y'
month_format
=
'%m'
paginate_by
=
50
make_object_list
=
True
class
BackupDayArchive(DayArchiveView):
allow_empty
=
True
allow_future
=
False
context_object_name
=
'list'
template_name
=
'backuplist.jinja.html'
http_method_names
=
[u
'get'
,]
model
=
Backup
date_field
=
'checkdate'
year_format
=
'%Y'
month_format
=
'%m'
day_format
=
'%d'
paginate_by
=
50
make_object_list
=
True
|
date_field:这个参数要指定model里面的日期型字段,是视图按这个字段过滤结果的
year_format month_format day_format:这几个是匹配URL捕捉的格式
make_object_list:我也不知道啥意思
urls.py
1
2
3
4
|
url(r
'^backuplist/$'
, BackupTodayArchive.as_view(), name
=
'backuplist'
),
url(r
'backuplist/(?P<year>\d{4})/$'
, BackupYearArchive.as_view()),
url(r
'backuplist/(?P<year>\d{4})/(?P<month>\d{2})/$'
, BackupMonthArchive.as_view()),
url(r
'backuplist/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$'
, BackupDayArchive.as_view()),
|
这样就完成一个按日期显示内容的功能:
显示当天数据:http://127.0.0.1/saadmin/backuplist/
显示2014年数据:http://127.0.0.1/saadmin/backuplist/2014/
显示2014年7月份的数据:http://127.0.0.1/saadmin/backuplist/2014/07/
显示2014年7月5日的数据:http://127.0.0.1/saadmin/backuplist/2014/07/05/
感觉挺好玩的,这几个视图。。
本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1438479如需转载请自行联系原作者
lihuipeng