gpt4 book ai didi

python - Django 的时区

转载 作者:太空宇宙 更新时间:2023-11-03 17:30:04 25 4
gpt4 key购买 nike

我开发了包含不同事件的网站。我想以用户本地时间显示事件开始时间。我不知道该怎么做。我尝试使用这个 django 应用程序 django-easy-timezones 。但它显示的是 UTC 时间。我的设置.py

TIME_ZONE = 'UTC'
USE_TZ = True

我的模板:

{% load tz %}
{% localtime on %}
{{ start_time|date:"H:i" }}
{% endlocaltime %}

可以在不询问时区的情况下做到这一点吗?

最佳答案

在不直接询问用户的情况下,只有三种方法可以实现这一点: 1) 使用客户端应用程序,2) 依赖地理 IP 或 3) 依赖浏览器专有位置服务。

选项 1 需要在客户端获取系统时区并通过请求返回它。在 javascript 中,您可以通过调用 getTimezoneOffset() http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp 来获取时区。 。

选项 2 将依赖 maxmind 的 GeoIP 数据库。他们有关于这个主题的常见问题解答http://dev.maxmind.com/faq/how-can-i-determine-the-timezone-of-a-website-visitor

The GeoIP2 City database similarly provides the time zone in the location record. To find out how to access this, refer to the documentation for your language’s GeoIP2 API.

选项 3 需要利用每个浏览器的专有地理定位技术。以下是如何使用 Mozilla https://www.mozilla.org/en-US/firefox/geolocation/ 执行此操作的链接

关于python - Django 的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31996466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com