Some background info first. Here are my Django settings:
USE_I18N = True
USE_L10N = True
USE_TZ = True
TIME_ZONE = "UTC"
And you should know that I am in the Amsterdam timezone, UTC+1
.
I don’t really understand the way Django handles its DateTimeField
in the admin forms. Like, the warning “Note: You are 1 hour ahead of server time.” is so incredibly unhelpful. It doesn’t make it clear if I should enter dates in my local time or in UTC. Why doesn’t it just say “Date and time must be entered as UTC”? That would make it super clear how I should enter the values, which the note does not.
At the moment I am adding help_text=f"Date and time must be entered as {settings.TIME_ZONE}."
to all my DateTimeField
instances, and hiding Django’s note with the following css:
.timezonewarning {
display: none;
}
Because our admins just don’t know what to enter in these fields with the default note.
And then when these values are shown in the admin interface, it shows them in UTC as well, rather than translated to the browser’s timezone. Which is fine for me, but why? Why even show that warning then? For example, I enter 2024-11-28 15:00:00
into one of the my DateTimeFields. This is entered as a UTC datetime, right? In the database it’s stored as 2024-11-28 16:00:00+01
, which is indeed 15:00 in UTC. (Weird that it doesn’t store it that way, but ok.)
And then when displaying this value in the admin, it shows Nov. 28, 2024, 3 p.m.
. Why does it not show 4 p.m.
, aka my local timezone? Why show that weird note about being ahead of server time? And why is the time not stored with +00
in the db?
Django doesn’t know my user’s timezone, since this is not something that’s stored in the User
model. But, it can use the browser’s timezone to show these dates and times in the local timezone, right? Or another option: add a timezone picker to the top menu, next to the theme switcher for example.
The docs say this:
When support for time zones is enabled, Django stores datetime information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user’s time zone in templates and forms.
So I guess I just don’t really understand the logic behind the timezone handling, and why it shows UTC everywhere in the admin.
I see two clear improvements here:
- Show the timezone that the date/time is supposed to be entered in, as supposed to showing “You are X hours ahead of server time." Seriously, nobody on my team knew what to enter in these fields until I changed the
help_text
and hid that warning. - When showing the value of this field, for example as part of
list_display
, either show the date/time translated to the browser’s timezone, or show the timezone. So instead of showingNov. 28, 2024, 3 p.m.
, showNov. 28, 2024, 3 p.m. (UTC)