Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I activate/deactivate the current timezone in Django (1.4)?

Tags:

django

So Django 1.4 just was released with time zone support, but I'm confused with how and when to utilize the "current time zone" that the docs keep mentioning. When should I activate and deactivate the current time zone for a user?

I'm pretty new with Django, so I'm not even sure if the context of the current time zone would apply to a specific user or to the web server (spanning all users). Any clarification on this would be great.

like image 254
fangsterr Avatar asked Mar 31 '12 19:03

fangsterr


1 Answers

New functionality in Django 1.4 makes rendering user's local time/date in your django templates at bit easier.

First of all, you'd need to set up your TIME_ZONE/USE_TZ parameters.

Then, in order to use "current time zone" functionality you need to know user's timezone. Probably the most reliable way would be to ask the user directly and save this information in user profile/session. Also, you can try setting timezone cookie via javascript by utilizing getTimezoneOffset() function or try to do some geoip magic and figure timezone by location.

Once you know user's timezone value, you can activate it in your middleware:

class MyTimezoneMiddleware(object):
    def process_request(self, request):
        user_timezone = request.session.get('current_timezone')

        if user_timezone:
             timezone.activate(user_timezone)
        else:
             timezone.deactivate()
like image 50
BluesRockAddict Avatar answered Sep 22 '22 08:09

BluesRockAddict