【问题标题】:Timezone is UTC infact of changing it in settings.py file时区是 UTC 实际上是在 settings.py 文件中更改它
【发布时间】:2020-07-09 19:21:06
【问题描述】:

当我在 Heroku 调度程序中看到它时,我的应用程序将 timezone.now() 显示为 UTC 时间。我还构建了一个自定义的 manage.py 命令来测试它,但它仍然显示 UTC 时间。

这是我的 settings.py

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True

这是我的个性化 manage.py 命令 managemet/commaands/abhiwaqt.py

from django.core.management.base import BaseCommand, CommandError
from django.apps import apps
from Directory.utils import *

class Command(BaseCommand):
    help="Refresh models and its fields."

    def handle(self,*args, **options):
        try:
            print(timezone.now())
            
        except:
            raise CommandError("Something went wrong")

设置 TIME_ZONE='Asia/Kolkata' 我遇到了这个问题。

【问题讨论】:

    标签: django heroku timezone


    【解决方案1】:

    保持后端使用 UTC 可能是一个好习惯,这样一切都是统一的并处理夏令时,您只需在 templatesviews 中向用户显示时处理更改。如果您想要时区支持(因为默认情况下禁用),请使用设置USE_TZ = True。查看文档以获取有关此主题的更深入解释。enter link description here

    【讨论】:

    • 嘿,但是我的系统现在根据我的时区当前时间的逻辑进行缩放。我也一直保持 USE_TZ=True 错误仍然存​​在。
    • 哦,所以在您的整个项目中,您将时间视为您的时区而不是 UTC?最好的办法是更改所有这些逻辑,您需要做的就是创建一个函数,该函数需要 UTC 时间并返回您的本地时间。然后检查您的代码并用该函数替换所有获取日期时间的调用。这将确保您没有任何夏令时问题等。
    • 好的,这就是最后一个选项!谢谢
    猜你喜欢
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 2012-06-05
    • 2019-04-12
    相关资源
    最近更新 更多