为什么时间仍然显示为 UTC 时区而不是 settings.TIME_ZONE?
Why does time still show with UTC time zone instead of settings.TIME_ZONE?
我有一个模型,它在模型的 __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 admin 时,我在标题中看到 Task: Scheduled at September 30 2018 12:30 AM 并在输入中填入正确的 TIME_ZONE
:20:30:00
settings.py
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
Django 在 displaying values with templates. If you want to do the conversion in arbitrary blocks of code, use the localtime()
helper:
时转换日期时间
from django.utils.timezone import localtime
def __str__(self):
return "Scheduled at %s" % localtime(self.date_time).strftime("%B %d %Y %I:%M %p")
我有一个模型,它在模型的 __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 admin 时,我在标题中看到 Task: Scheduled at September 30 2018 12:30 AM 并在输入中填入正确的 TIME_ZONE
:20:30:00
settings.py
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
Django 在 displaying values with templates. If you want to do the conversion in arbitrary blocks of code, use the localtime()
helper:
from django.utils.timezone import localtime
def __str__(self):
return "Scheduled at %s" % localtime(self.date_time).strftime("%B %d %Y %I:%M %p")