问题描述
我想知道是否有办法从客户端自动检索用户的时区。特别是在登录期间。
I would like to know if there is a way to automatically retrieve user's timezone from client. Especially during login.
我尝试在登录页面中添加(使用 auth.login
):
I tried to add this in the login page (using auth.login
):
{% get_current_timezone as TIME_ZONE %}
然后将其添加到登录表单
and then add this in the login form
<input type="hidden" name="next" value="/redirect/?tz={{ TIME_ZONE }}">
但 tz
始终是
推荐答案
从:
当前时区是翻译当前语言环境的
当量。但是,没有
相当于Django可以使用的Accept-Language HTTP标头来
自动确定用户的时区。相反,Django提供
时区选择功能。使用它们构建时间段
选择逻辑,这对您是有意义的。
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.
您可以尝试通过javascript设置时区cookie通过使用功能,或尝试按位置来做一些geoip魔术和图形时区。最可靠的方法可能是直接询问用户,并将该信息保存在用户个人资料/会话中。
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.
这篇关于django 1.4如何从客户端自动获取用户的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!