Hi, I might have a suggestion to Tutorial 02 when it says python manage.py shell
runs python taking settings.py into account. Later on, it suggests using timezone.now()
for the Question instantiation (mind you timezone is imported with from django.utils import timezone
).
I would expect that timezone.now()
would bring up the time in the timezone I set in settings.py, but it has not. It is not much intuitive because the learner is led to think they are using the settings.py, while only a timezone.localtime()
would bring up the timezone. I hence suggest we had a note that says timezone.localtime()
will output the time with the settings.
On the other hand, I believe there could be a reason why timezone.now()
is used in the tutorial, but I cannot see it — maybe the best practice with databases in Django is to keep all in UTC to avoid timezone conversion errors? — Well, if that is the case, I suspect a little hint would come in handy.
It is a common practice to only store UTC time in your database, and then convert it for display as necessary.
It’s not just a “best practice for Django” - it’s a best practice overall - precisely because of the potential problems with timezone conversion / DST
The very first paragraph in the docs for Time zones
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.
and then in the third paragraph:
Even if your website is available in only one time zone, it’s still good practice to store data in UTC in your database. …
Also in the Playing with the API section of that page, there’s this note that references the above docs:
Note the addition of import datetime
and from django.utils import timezone
, to reference Python’s standard datetime
module and Django’s time-zone-related utilities in django.utils.timezone
, respectively. If you aren’t familiar with time zone handling in Python, you can learn more in the time zone support docs.
1 Like
Oh yeah, sure. I actually might have read it there yesterday night, but forgot it.
Thank you very much for the quick reply, Ken!