博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django timezone问题
阅读量:6082 次
发布时间:2019-06-20

本文共 3382 字,大约阅读时间需要 11 分钟。

hot3.png

今天用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.

马上安装了pytz,问题依旧。又搜了下mysql

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,终于正常了。

转载于:https://my.oschina.net/quanpower/blog/210826

你可能感兴趣的文章
Why Namespace? - 每天5分钟玩转 OpenStack(102)
查看>>
Nginx 常用全局变量
查看>>
一个5年运维工程师的新年回首
查看>>
分享30个高品质的抽象网页背景素材
查看>>
Web前端开发人员和设计师必读文章推荐【系列八】
查看>>
为工程添加组件+改写JSP页面为HTML文件
查看>>
Linux下装db2
查看>>
CentOS 7.3 关于系统启动级别
查看>>
【备忘】bash 脚本 拼 mysql 语句
查看>>
eureka相关配置
查看>>
给路由器设置enable密码[神州数码实现]
查看>>
我的友情链接
查看>>
烂泥:使KVM显示VM的IP地址及主机名
查看>>
MyBatis的flushCache和useCache的使用注意
查看>>
关于ios的autoLayout的一些简单介绍以及使用方法
查看>>
IOS学习路线(2014-05-08)
查看>>
Java正则表达式字符串匹配示例
查看>>
我的友情链接
查看>>
18本很好的HTML5电子书学习资料
查看>>
Linux中最危险的10个命令
查看>>