Django expects to work with timezone aware datetime objects.
From the Django documentation, the now()
call would become:
import datetime
from django.utils.timezone import utc
now = datetime.datetime.utcnow().replace(tzinfo=utc)
expires = now + datetime.timedelta(hours=7)
Better still, use the now()
function from django.utils.timezone
.
Both datetime.now()
and datetime.utcnow()
return naive datetime
objects which is not what Django requires. With a time zone applied to expires
, Django is then able to convert back to UTC for storage as UTC in the database backend.
The NonExistentTimeError
is actually thrown by code from the pytz
module. The pytz documentation is definitely worth reading as there's many gotchas when dealing with time zones.