I'm a django newbie and have been looking at this timezone stuff for several days now, and I'm still confused. I now have a timezone-aware datetime value stored in the postgres db in UTC. However, when I present it to a user on a template, I want it to be presented in the user's timezone. I have a field in a model that specifies the user's timezone, and I can get at it through field = request.user.get_profile() in a view (I'm using django profiles). But I'm still not sure how to use it in a template to show the user's local time. I tried {{ value.date_time_field | timezone=field.timezone }} but that doesn't work.
Someone suggested I should get at this in the view instead of trying to do it in the template. I'm not sure.
Any help is greatly appreciated.
Thanks!
Django finally got timezone functionality in version 1.4. Although you want to make sure pytz is installed.
All datetime values will be stored in the DB as UTC. When you display a datetime value in a template, it will be localized to whatever the 'Active' timezone is. By default, unless you activate something else, it will use the TIME_ZONE setting from your settings file.
If you want to activate another timezone, just call timezone.activate. For example:
from django.utils import timezone
timezone.activate('US/Pacific')
If you want to automate this, just write a custom middleware:
from django.utils import timezone
class TimezoneMiddleware(object):
def process_request(self, request):
timezone.activate(request.user.get_profile().timezone)
Then make sure to include your TimezoneMiddleware in the MIDDLEWARE_CLASSES in settings.py.