settings.py
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
改為
# 把英文改為中文
LANGUAGE_CODE = 'zh-hans'
# 把國際時區(qū)改為中國時區(qū)
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))
+++++++++++++++++++++++++++++++++++++++++++++++++++
把需要搜索的日期從數(shù)據(jù)庫中找出來
views.py
date_list = models.Host.objects.dates('create_date', 'month', order='DESC')
date_list在前端循環(huán)
+++++++++++++++++++++++++++++++++++++++++++++++++
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{% for date in date_list %}
<a href="/archive/{{ date.year }}/{{ date.month }}/">{{ date.year }}年{{ date.month }}月<br></a>
{% endfor %}
根據(jù)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="/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>