settings.py
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
改为
# 把英文改为中文
LANGUAGE_CODE = 'zh-hans'
# 把国际时区改为中国时区
TIME_ZONE = 'Asia/Shanghai'
models.py
from datetime import datetime
import pytz
tz = pytz.timezone('Asia/Shanghai')
class Host(models.Model):
create_date = models.DateTimeField(default=datetime.now(tz))
+++++++++++++++++++++++++++++++++++++++++++++++++++
把需要搜索的日期从数据库中找出来
views.py
date_list = models.Host.objects.dates('create_date', 'month', order='DESC')
date_list在前端循环
+++++++++++++++++++++++++++++++++++++++++++++++++
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{% for date in date_list %}
<a href="//www.greatytc.com/archive/{{ date.year }}/{{ date.month }}/">{{ date.year }}年{{ date.month }}月<br></a>
{% endfor %}
根据urls.py
url(r'^archive/(?P<year>\d+)/(?P<month>\d+)/$', views.archive, name="archive"),
传入views.py
def archive(request, year, month):
host_list = models.Host.objects.filter(create_date__year=year, create_date__month=month)
return render(request, 'index.html', {'host_list': host_list})
再在前端index.html打印host_list,这个host_list就是要查找的主机
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{% for date in date_list %}
<a href="//www.greatytc.com/archive/{{ date.year }}/{{ date.month }}/">{{ date.year }}年{{ date.month }}月<br></a>
{% endfor %}
+++++++++++++++++++++++++++++++++++++++++++++++
{% for host in host_list %}
{{ host.hostname }}
<br>
{{ host.create_date }}
{% endfor %}
+++++++++++++++++++++++++++++++++++++++++++++++
</body>
</html>