Django似乎以UTC格式显示日期时间 [英] Django seems to be displaying datetimes in UTC format

查看:292
本文介绍了Django似乎以UTC格式显示日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

settings.py 中,我有:

TIME_ZONE = 'Asia/Singapore'
USE_I18N = True
USE_L10N = True
USE_TZ = True

如果用户(居住在新加坡)在我的网站上的表单中输入 2013-10-07 01:00 AM ,则该值存储在我的( PostgreSQL)数据库为 2013-10-07 01:00:00 + 08 。在 python manage.py shell 会话期间提取此信息时,得到 2013-10-06 17:00:00 + 00:00 。当我尝试在模板中呈现此信息时,也会发生同样的情况。

If a user (who is living in Singapore) enters 2013-10-07 01:00 A.M. in a form on my site, the value stored in my (PostgreSQL) database is 2013-10-07 01:00:00+08. When I pull up this information during a python manage.py shell session, I get 2013-10-06 17:00:00+00:00. The same happens when I try to render this information in a template.

我正在发生的事情:Django识别出用户正在输入1:00。 (新加坡时间10月10日),并将其存储在数据库中,作为 2013-10-07 01:00:00 + 08 。但是,当Django从数据库中检索此信息时,会将其格式化为UTC时间,从而给出 2013-10-06 17:00:00 + 00:00

What I think is happening: Django recognizes that the user is entering 1:00 A.M. on October 10th, Singapore time, and stores this in the database as 2013-10-07 01:00:00+08. However, when Django retrieves this info from the database, it formats it to UTC time, thereby giving 2013-10-06 17:00:00+00:00.

我有权利吗?如果是这样,我该如何使用数据库中存储的相同时区信息(或至少使用我的 TIME_ZONE 设置)使Django显示时间?换句话说,如何使日期时间与用户输入的日期时间完全相同?

Do I have that right? And if so, what can I do to make Django display times using the same timezone information that is stored in the database (or at least using my TIME_ZONE setting)? In other words, how can I make it so that the user sees the datetime in the exact same form as she entered it?

推荐答案

我已经知道发生了什么事。根据我在文档此处,我假设使用 USE_TZ = True ,Django将在当前时区输出日期时间(默认为 TIME_ZONE 设置)无处不在-视图,shell等。

I've figured out what's going on. Based on what I had read in the docs here, I was assuming that with USE_TZ=True, Django would output datetimes in the current time zone (which defaults to the TIME_ZONE setting) everywhere--views, the shell, etc.

但是,事实证明,Django only 在模板中进行转换,即使如此,也仅在 datetime 对象的最直接,最基本的调用中进行转换。

However, it turns out that Django only makes the conversion in templates, and even then only in the most direct, basic invocation of a datetime object.

特别是,如果您有对象 DateTimeField ,然后使用 {{object.datetime}} 在模板中呈现其 datetime 属性,您将获得一个日期时间转换为当前时区。好极了。但是,即使在模板中,例如 {{object.datetime.hour}} (它将在其他任何模板中),此功能也无法用于其他任何功能。以UTC显示小时)。因此,它基本上只是一个不可见的模板标签。

Specifically, if you have an object with a DateTimeField and you render its datetime attribute in a template using {{ object.datetime }}, you will get a datetime converted to the current time zone. Yay. However, this functionality won't work for anything else, even within templates, such as {{ object.datetime.hour }} (which will display the hour in UTC). So it's basically just an invisible template tag. Not as magical as I'd hoped!

看起来我需要将所有日期时间转换为视图中的当前时区,然后再将其传递到模板中。考虑到我的数据库已经已经将所有日期时间存储在我希望它们显示的时区中,我发现这种怪异和违反直觉的事情。必须明确地告诉Django您,这更有意义吗?想要用UTC表示的数据库值,而不是让Django自动执行工作,然后让您在视图中更改它们?

It looks like I'll need to convert all datetimes to the current time zone in my views before passing them to my templates. I find this kind of weird and counterintuitive considering that my database already has all of the datetimes stored in the time zone I want them displayed in. Wouldn't it make more sense to have to explicitly tell Django you want database values expressed in UTC, than to have Django perform the work automatically and then make you change them back in your views?

编辑: SO上的答案使针对我的具体情况的解决方案变得相当容易:

This answer on SO made the solution to my specific situation considerably easier:

from django.utils.timezone import localtime

result = localtime(some_time_object)

编辑:事实证明,只有PostgreSQL存储时区信息,并且该信息与其存储的原始日期时间值(以UTC表示)是分开的。因此,我认为对于Django默认情况下以UTC呈现所有内容是有意义的,因为其他数据库后端甚至都不存储时区信息。

It turns out that only PostgreSQL stores time zone information, and that information is separate from the raw datetime values it stores, which are in UTC. So I guess it makes sense for Django to render everything in UTC by default, since other database backends don't even store time zone info.

这篇关于Django似乎以UTC格式显示日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆