【问题标题】:Django timezone not working with postgresqlDjango 时区不适用于 postgresql
【发布时间】:2019-08-27 03:49:26
【问题描述】:

我想在我的 django 应用程序上显示用户时区。该数据库位于 postgresql 服务器上。使用下面的配置无法达到我的目标。

Django 端:


TIME_ZONE = 'UTC'

USE_L10N = True

USE_TZ = True

这是我的日期字段:

created = models.DateTimeField(auto_now =True)

Postgres 端: 在我的数据库中,“created”字段似乎知道时区:

\d my_table;
Column      |           Type  
created     | timestamp with time zone 

这是存储在此字段中的数据示例。它存储用户的时区,而不是 UTC:

select created from my_table;
            created
-------------------------------
 2019-02-03 10:05:40.462164+02

附加信息:
- pytz 已安装。
- 我的 django 应用显示 UTC 时间。

我的问题: 如何向用户显示时区?你看我的配置有问题吗?

非常感谢。

【问题讨论】:

标签: django postgresql timezone


【解决方案1】:

您需要将 Postgresql 时区更改为 UTC。 命令:

ALTER USER User_Name SET TimeZone TO 'utc';

并重启 postgres 服务:

sudo service postgresql restart

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2015-12-26
    • 2012-04-14
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多