django 1.4 how to automatically get user#39;s timezone from client(django 1.4 如何从客户端自动获取用户的时区)
问题描述
我想知道是否有办法从客户端自动检索用户的时区.尤其是在登录时.
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
始终是服务器的时区.
but tz
is always the timezone of the server.
推荐答案
来自 文档:
选择当前时区
当前时区是相当于翻译的当前语言环境.然而,没有相当于 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.
您可以尝试使用 getTimezoneOffset 功能或尝试做一些 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 如何从客户端自动获取用户的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:django 1.4 如何从客户端自动获取用户的时区


基础教程推荐
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01