The Django documentation for timezones documents all the necessary details for converting datetime
objects to the appropriate time zone for display.
Your data is stored in UTC which is good. When you obtain a DateTime
field object from the database it will be a naive datetime.datetime
object. ie A date/time without a timezone attached. It's then up to you to do the conversion.
User of your webapp may be in different time zones so the conversion to an appropriate time zone must occur for each request. This is why there is an activate function to set the current time zone.
If you have pytz installed you should be able to do the following:
from django.utils.timezone import activate
activate(settings.TIME_ZONE)
All output of date field in the template engine will then automatically convert you naive date time objects to the correct time zone for display.
If you just have a single naive datetime.datetime
instance that you want to set the time zone on, then just use the pytz
module directly. It is not normal to do this in your views though, as it's a good idea to only convert the time zone at the point of presentation.
from pytz import timezone
settings_time_zone = timezone(settings.TIME_ZONE)
last_updated = last_updated.astimezone(settings_time_zone)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…