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

前端 未结 1 638
遥遥无期
遥遥无期 2021-01-26 05:06

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.strfti         


        
相关标签:
1条回答
  • 2021-01-26 05:51

    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")
    
    0 讨论(0)
提交回复
热议问题