时间:2021-07-01 10:21:17 帮助过:18人阅读
models.Article中create_time定义如下:
create_time = models.DateTimeField(u‘创建时间‘, auto_now_add=True)
设置后,后台呈现效果:
按照时间按分层筛选时,出现了错误:
ValueError at /admin/blog/article/
Database returned an invalid datetime value. Are time zone definitions for your database installed?
Request Method: GET
Request URL: http://127.0.0.1:8800/admin/blog/article/?create_time__year=2017
Django Version: 1.11.5
Exception Type: ValueError
Exception Value:
Database returned an invalid datetime value. Are time zone definitions for your database installed?
Exception Location: C:\Python27\lib\site-packages\django\db\models\functions\datetime.py in convert_value, line 198
Python Executable: C:\Python27\python.exe
Python Version: 2.7.13
Python Path:
[‘E:\\Work\\django\\myblog\\apps‘,
‘E:\\Work\\django\\myblog‘,
‘E:\\Work\\django\\myblog‘,
‘E:\\Work\\django\\myblog\\apps‘,
‘C:\\Windows\\system32\\python27.zip‘,
‘C:\\Program Files\\VisualSVN Server\\bin‘,
‘C:\\Program Files\\VisualSVN Server\\PythonPackages‘,
‘C:\\Python27\\DLLs‘,
‘C:\\Python27\\lib‘,
‘C:\\Python27\\lib\\plat-win‘,
‘C:\\Python27\\lib\\lib-tk‘,
‘C:\\Python27‘,
‘C:\\Python27\\lib\\site-packages‘]
Server time: 星期三, 27 九月 2017 15:01:23 +0800
首先需要确认数据的时区设置没有问题,这里使用的是mysql数据库,时区设置如下(都是依赖计算机系统的时区):
mysql> show variables like ‘%time_zone%‘; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | | | time_zone | SYSTEM | +------------------+--------+ 2 rows in set, 1 warning (0.01 sec) mysql> select now(); +---------------------+ | now() | +---------------------+ | 2017-09-27 14:59:19 | +---------------------+ 1 row in set (0.00 sec) mysql> SELECT @@global.time_zone, @@session.time_zone; +--------------------+---------------------+ | @@global.time_zone | @@session.time_zone | +--------------------+---------------------+ | SYSTEM | SYSTEM | +--------------------+---------------------+ 1 row in set (0.00 sec) mysql>
其次,修改django project下的settings.py中市区配置信息:
# LANGUAGE_CODE = ‘en-us‘ # TIME_ZONE = ‘UTC‘ LANGUAGE_CODE = ‘zh-hans‘ TIME_ZONE = ‘Asia/Shanghai‘ USE_I18N = True USE_L10N = True #USE_TZ = True USE_TZ = False
备注:
1)为了汉化设置了本地语言编码为LANGUAGE_CODE=‘zh-hans’,注意我这里是django1.11.1版本,如果是django1.8之前的版本不是这么设置了的而是LANGUAGE_CODE = ‘zh-cn‘
2)时区默认设置是‘UTC’,我这里为了和本地一样我设置为‘Asia/Shanghai’
3)默认USE_TZ=True,这也是问题的关键,直接修改为USE_TZ=False
参考:http://blog.csdn.net/w6299702/article/details/38782607
启用 USE_TZ = True 后,处理时间方面,有两条 “黄金法则”:
比如,通常获取当前时间用的是:
import datetime now = datetime.datetime.now()
启用 USE_TZ = True 后,需要写成:
import datetime from django.utils.timezone import utc utcnow = datetime.datetime.utcnow().replace(tzinfo=utc)
模板
除非应用支持用户设置自己所在的时区,通常我们不需要关心模板的时区问题。模板在展示时间的时候,会使用 settings.TIME_ZONE 中的设置自动把 UTC 时间转成 settings.TIME_ZONE 所在时区的时间渲染。
TIME_ZONE = ‘Asia/Shanghai‘
Django(博客系统):按照时间分层筛选“/blog/article/?create_time__year=2017”,出现问题:Database returned an invalid datetime value. Are time zone definitions for your database installed?
标签:分享 cti 获取 没有 image settings ima 技术 zip