django按日期日期搜索

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




    
    


{% for date in date_list %}
{{ date.year }}年{{ date.month }}月
{% endfor %}

根据urls.py

url(r'^archive/(?P\d+)/(?P\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就是要查找的主机




    
    


{% for date in date_list %}
{{ date.year }}年{{ date.month }}月
{% endfor %} +++++++++++++++++++++++++++++++++++++++++++++++ {% for host in host_list %} {{ host.hostname }}
{{ host.create_date }} {% endfor %} +++++++++++++++++++++++++++++++++++++++++++++++

你可能感兴趣的:(django按日期日期搜索)