为什么时间仍然显示UTC时区而不是settings.TIME_ZONE?

问题描述 投票:0回答:1

我有一个模型,在模型的__str__()方法中显示一个短字符串

def __str__(self):
    return "Scheduled at %s" % (self.date_time.strftime("%B %d %Y %I:%M %p"))
    #Output: <Task: Scheduled at September 30 2018 12:30 AM>
    # it should have been: <Task: Scheduled at September 29 2018 08:30 PM>

当我去Django管理员时,我看到标题任务:预定于2018年9月30日上午12:30,在输入中它充满了正确的TIME_ZONE:20:30:00

settings.朋友

TIME_ZONE = 'Etc/GMT+4'

USE_I18N = False

USE_L10N = False

USE_TZ = True

他一直用UTC时区显示时间,但是我在我的设置中设置了TIME_ZONE='Etc/GMT+4

我希望时间数据以UTC格式保存在数据库中,并使用TIME_ZONE显示它们,在我的例子中Etc/GTM+4

python django datetime pytz django-timezone
1个回答
2
投票

displaying values with templates时,Django转换日期时间。如果要在任意代码块中进行转换,请使用localtime()帮助器:

from django.utils.timezone import localtime

def __str__(self):
    return "Scheduled at %s" % localtime(self.date_time).strftime("%B %d %Y %I:%M %p")
© www.soinside.com 2019 - 2024. All rights reserved.