Форма django: сообщение об ошибке с указанием даты и времени
Я пытаюсь добавить дату и время в свое сообщение об ошибке формы. К сожалению, оно отображается в UTC (мой TIME_ZONE), но должно быть в текущем часовом поясе.
def clean(self):
# print(get_current_timezone()) > Europe/Berlin
cleaned_data = super(LegForm, self).clean()
departure_scheduled = cleaned_data.get('departure_scheduled')
# check departure in journey
if departure_scheduled < self.instance.journey.start:
journey_start = localize(self.instance.journey.start)
# print(self.instance.journey.start.tzinfo) > UTC
self.add_error(
'departure_scheduled',
_('Error Message (%(start)s).') % {'start': journey_start}
)
print(get_current_timezone())
возвращается Europe/Berlin
print(self.instance.journey.start.tzinfo)
возвращается UTC
Текущий часовой пояс активирован. Есть ли равно localize()
преобразовать объект datetime в текущий часовой пояс?
1 ответ
Я мог бы использовать:
journey_start = localize(self.instance.journey.start.astimezone(get_current_timezone()))
Но похоже на борьбу с поддержкой часового пояса Джанго.
ОБНОВЛЕНИЕ:
Спасибо комментарию Кевина Кристофера Генри, вот инструмент django, который я искал:
from django.utils import timezone
journey_start = timezone.localtime(self.instance.journey.start)
https://docs.djangoproject.com/en/dev/topics/i18n/timezones/