Django timezone问题

今天用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 pytz documentation discusses these issues 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 USE_TZ = True in your settings file. Installing pytz is highly recommended, but not mandatory. It’s as simple as:

$ sudo pip install pytz

Note

The defaultsettings.pyfile created by django-admin.py startproject includes USE_TZ = True for convenience.

Note

There is also an independent but related USE_L10N setting that controls whether Django should activate format localization. See Format localization for more details.

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

4.4.6 mysql_tzinfo_to_sql — Load the Time Zone Tables

The mysql_tzinfo_to_sql 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 Section 10.6, “MySQL Server Time Zone Support”.

mysql_tzinfo_to_sql 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 mysql_tzinfo_to_sql and send the output into the mysql program. For example:

shell> mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql

mysql_tzinfo_to_sql reads your system‘s time zone files and generates SQL statements from them. mysql processes those statements to load the time zone tables.

The second syntax causes mysql_tzinfo_to_sql 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 mysql_tzinfo_to_sql 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 mysql_tzinfo_to_sql, 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,终于正常了。

时间: 2024-08-09 04:42:38

Django timezone问题的相关文章

[转] datetime.now、datetime.utcnow以及Django中的timezone.now之间的区别

https://blog.csdn.net/sinat_41898105/article/details/80984298 在我们使用Django做项目时经常会遇到时间的存储与读取不一致的问题,针对这个问题,我在查阅了一些资料,现总结一下给大家分享. 由于现在使用的django大都在1.4版本以后,所以就存在两个概念naive time与active time.naive time就是不带时区的时间,active time就是带时区的时间.例如, 使用datetime.datetime.utcn

django入门 -- 简单流程

django入门 -- 简单流程 这里简单的演示完成一个 django 大概流程,后面会有详细解释 搭建开发环境 以下使用的是 ubuntu 系统 在生产环境中,有的时候,我们开发的项目可能用的是几个不同的 环境,所以通常我们会创建虚拟环境,在虚拟环境中安装我们需要的 配置,这里从配置虚拟环境开始 创建虚拟环境 创建:mkvirtualenv [虚拟环境名称] 删除:rmvirtualenv [虚拟环境名称] 进入:workon [虚拟环境名称] 退出:deactivate 所有的虚拟环境,都位

Django之ORM操作

ORM基本增删改查操作: 1 # 增 2 models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs 3 obj = models.Tb1(c1='xx', c2='oo') 4 obj.save() 5 6 # 查 7 models.Tb1.objects.get(id=123) # 获取单条数据,不存在则报错(不建议) 8 models.Tb1.objects.all() # 获取全部 9 models.Tb1

Django基础

一.创建数据库 1.在APP下的models创建表,django可以自动创建表,不可以自动创建数据库 class userinfo(models.Model): username = models.CharField(max_length=50) password = models.CharField(max_length=50) 2.连接数据库,在settings.py下更改数据库的配置信息和应用的APP,Django项目建成后, 默认设置了使用SQLite数据库, 在my_blog/my_b

Django 应用开发(2)

1.创建一个管理员用户 创建一个用于登录管理站点的用户 2.启动开发服务器 python manage.py runserver 打开浏览器访问本地域名中的/admin/ 3.让poll应用在管理站点中可编辑 在polls/admin.py中编辑 4.修改界面 在polls/admin.py中编辑 1 from django.contrib import admin 2 from .models import Choice,Question 3 4 class ChoiceInline(admi

frist Django app — 五、Test

Test——很重要但是没有被重视起来的一个环节,至少是我自己,其实自己之前在做java web的时候就去尝试过怎么做REST接口的测试,一直没有找到一种合适方式,而且因为时间紧没有进一步深究,但是造成的后果每次做了修改之后都测试不充分,引起新的问题,所以这次对于python正好看看Django的单元测试. 用的是单独的数据库,数据库是干净的(暂未有数据库,test所有操作都是从零开始),不会对正式的数据库造成影响 Test Model 到现在我们主要的业务逻辑代码在model和view里面,所以

frist Django app— 二、 Model和管理界面

Django是符合MVC架构的,这里现学习M—Model,而且Django自带了一个管理model(数据库)的界面,所以一并学习. Database 配置 编辑Django的配置文件settings.py进行配置 添加polls app,修改后如下 INSTALLED_APPS = [ 'django.contrib.admin', # 管理界面 'django.contrib.auth', # 认证系统 'django.contrib.contenttypes', # 框架的content t

django 投票小项目

创建一个mysite项目 django-admin startproject mysite 创建一个polls应用 django-admin startapp polls mkdir polls/templates/ 创建模板文件夹 polls.views.py 内容如下 from django.shortcuts import get_object_or_404, render from django.core.urlresolvers import reverse from django.v

django复习笔记2:models

关于models,主要想说的是django shell以及生成测试数据的脚本这两部分 先看一个设置了外键的models from django.db import models from django.utils import timezone from django.contrib.auth.models import User class Post(models.Model): author = models.ForeignKey(User) title = models.CharField