问题描述:
最近在阿里云部署 Django(1.11.x) 时,在后台发布文章后,页面返回 500 异常。
刚开始的时候,遇到这个问题一脸懵逼,不知道该如何入手。后来把 settings.py 中 Debug=True ,然后运行网站,发现显示下面的错误信息:
Database returned an invalid datetime value. Are time zone definitions for your database installed?
看到这个信息后就知道该怎么入手解决了,手动点赞!
解决办法:
1. 确保 mysql 中相应时区设置为 SYSTEM
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() |+---------------------+| 2018-01-20 01:02:00 |+---------------------+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)
2. 修改 settings.py 中时区配置信息
LANGUAGE_CODE = 'zh-hans'TIME_ZONE = 'Asia/Shanghai'USE_TZ = False
运行网站,大功告成!