Question
I would like to know if there is a way to automatically retrieve user's timezone from client. Especially during login.
I tried to add this in the login page (using auth.login
):
{% get_current_timezone as TIME_ZONE %}
Then, add this in the login form:
<input type="hidden" name="next" value="/redirect/?tz={{ TIME_ZONE }}">
But, tz
is always the timezone of the server.
Answer
From the documentation:
Selecting the current time zone
The current time zone is the equivalent of the current locale for translations. However, there's no equivalent of the Accept-Language HTTP header that Django could use to determine the user's time zone automatically. Instead, Django provides time zone selection functions. Use them to build the time zone selection logic that makes sense for you.
You can try setting timezone cookie via javascript by utilizing getTimezoneOffset function or try to do some geoip magic and figure timezone by location. Probably the most reliable way would be to ask the user directly and save this information in user profile/session.