今天用django做个blog碰到了问题,提交内容后浏览提示Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?
django官方文档这么说:
When support for time zones is enabled, Django stores date and time information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user’s time zone in templates and forms.
This is handy if your users live in more than one time zone and you want to display date and time information according to each user’s wall clock.
Even if your Web site is available in only one time zone, it’s still good practice to store data in UTC in your database. One main reason is Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions happen. (The documentation discusses in greater detail.) This probably doesn’t matter for your blog, but it’s a problem if you over-bill or under-bill your customers by one hour, twice a year, every year. The solution to this problem is to use UTC in the code and use local time only when interacting with end users.
Time zone support is disabled by default. To enable it, set in your settings file. Installing is highly recommended, but not mandatory. It’s as simple as:
$ sudo pip install pytz
Note
The defaultsettings.pyfile created by includes for convenience.
Note
There is also an independent but related setting that controls whether Django should activate format localization. See for more details.
4.4.6 mysql_tzinfo_to_sql — Load the Time Zone Tables
The program loads the time zone tables in themysqldatabase. It is used on systems that have a zoneinfo database (the set of files describing time zones). Examples of such systems are Linux, FreeBSD, Solaris, and Mac OS X. One likely location for these files is the/usr/share/zoneinfodirectory (/usr/share/lib/zoneinfoon Solaris). If your system does not have a zoneinfo database, you can use the downloadable package described in .
can be invoked several ways:
shell> mysql_tzinfo_to_sql tz_dir shell> mysql_tzinfo_to_sql tz_file tz_name shell> mysql_tzinfo_to_sql --leap tz_file
For the first invocation syntax, pass the zoneinfo directory path name to and send the output into the program. For example:
shell> mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
reads your system's time zone files and generates SQL statements from them. processes those statements to load the time zone tables.
The second syntax causes to load a single time zone file tz_file that corresponds to a time zone name tz_name:
shell> mysql_tzinfo_to_sql tz_file tz_name | mysql -u root mysql
If your time zone needs to account for leap seconds, invoke using the third syntax, which initializes the leap second information. tz_file is the name of your time zone file:
shell> mysql_tzinfo_to_sql --leap tz_file | mysql -u root mysql
After running , it is best to restart the server so that it does not continue to use any previously cached time zone data.
于是先这样试了下:
shell> mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql有warning!强制执行
mysql_tzinfo_to_sql /usr/share/lib/zoneinfo | mysql -uroot --force mysql插进去了,重启了下mysql,终于正常了。