Datetime objects are stored in UTC and are converted to local timezone while displaying.
So when I call timezone.now()
, it should convert the UTC to local timezone and display the time accordingly.
I have installed pytz
as recommended by django.
I did the following :
Set USE_TZ = True
in settings.py
and in models.py
where I save the object to database
from django.utils import timezone
time= timezone.now()
Also did timezone.activate(pytz.timezone("America/Los_Angeles"))
When I fetch the stored time,it is 2014-10-01 00:50:37
, which is some hours ahead of present time.
Where am I going wrong?
The time stored in database was in UTC which is right. Problem was when I was retrieving it, it wasn't being converted to local timezone, it was printing in UTC. So i had to convert it in the template as below:
{% load tz %}
{{ value|timezone:"America/Los_Angeles" }}
Phew, it worked!