Why does time still show with UTC time zone instead of settings.TIME_ZONE?

本小妞迷上赌 提交于 2019-12-04 05:33:37

问题


I have a model that shows a short string in __str__() method of the model

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>

When I go to the Django admin, I see in the title Task: Scheduled at September 30 2018 12:30 AM and in the input it is filled with the correct TIME_ZONE: 20:30:00

settings.py

TIME_ZONE = 'Etc/GMT+4'

USE_I18N = False

USE_L10N = False

USE_TZ = True

He keeps showing time with UTC time zone, however I set TIME_ZONE='Etc/GMT+4 in my settings.

I want time data to be saved in database as UTC and show them with TIME_ZONE, in my case Etc/GTM+4


回答1:


Django converts datetimes when 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")


来源:https://stackoverflow.com/questions/52573184/why-does-time-still-show-with-utc-time-zone-instead-of-settings-time-zone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!