Answer the question
In order to leave comments, you need to log in
What is the correct way to display DateTimefield in Django?
In the settings is:
TIME_ZONE = 'Europe/Samara'
USE_TZ = True
class A(models.Model):
created = models.DateTimeField()
def __str__(self):
return 'Заявка от {}'.format(self.created.strftime('%d.%m.%Y %H:%M'))
Заявка от 15.05.2018 09:30
Заявка от 15.05.2018 13:30
def __str__(self):
return self.created
15 мая 2018 13:30
Answer the question
In order to leave comments, you need to log in
Django stores such objects in UTC, and applies the time zone at the time of rendering to the template. Those. you have to do it by hand.
from django.utils import timezone
tz = timezone.get_default_timezone()
class A(models.Model):
created = models.DateTimeField()
def __str__(self):
return 'Заявка от {}'.format(self.created.astimezone(tz).strftime('%d.%m.%Y %H:%M'))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question