0

In Jinja2, how would you specify a default rendering method for a certain type?

In particular, datetime?

I found it quite annoying when rendering datetime values from Django. They look like 2022-11-04T00:00:00.987654+00:00. What was that T for, and why there was a plus + followed by 00:00. My users who lived on small islands for their entire life wouldn't understand. Aside from the formatting problem, Django gives UTC time objects. Always UTC, despite the TIME_ZONE in its settings module has been specified with a different value.

I know I can use a filter thing like me.time_of_death|format_datetime. However putting it after every single datetime field sounds insane to me, and I don't want to be woken up in the midnight because of a datetime without that filter released on the previous day.

Is it possible to make it default?

neuront
  • 9,312
  • 5
  • 42
  • 71

1 Answers1

0

You can use dateparse:

from django.utils import dateparse

Then when before you pass the time to the template you can use the following to convert it to something more understandable to your fellow islanders:

readable_time = dateparse.parse_datetime(CONFUSING_TIME_STRING)
Andrew
  • 13
  • 5