Django 会自动检测最终用户的时区吗? [英] Does Django automatically detect the end user's timezone?

查看:23
本文介绍了Django 会自动检测最终用户的时区吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Django 中构建一个应用程序,它允许最终用户检索对特定日期的时间(上午 12 点到凌晨 12 点)敏感的信息.我将此信息作为整数存储在我的数据库中,以 30 分钟为增量表示自午夜以来的秒数.我在看 Django 的时区文档:https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/ 并发现自己对 Django 是否自动使用最终用户时间感到困惑,或者我是否必须收集这些信息并在我的视图中对其进行说明.

I am building an application in Django which allows the end-user to retrieve information which is sensitive to the time of day (12 am to 12 am) on a given day. I store this information in my database as an integer representing the seconds since midnight in 30-minute increments. I was looking at Django's timezone documentation: https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/ and found myself confused on whether or Django automatically uses the end-users time, or if I must collect this information and account for it in my views.

任何信息都会有帮助!谢谢.

Any information would be helpful! Thanks.

推荐答案

Django 不检测最终用户的时区.它以UTC时间保存数据库中的对象.

Django doesn't detect end user's timezone. It saves the objects in the database in UTC time.

然后就可以在浏览器上使用 JavaScript 将 UTC 时间转换为客户端的时间进行显示.

Then you can convert the UTC time to the client's time on the browser using JavaScript for displaying.

Django 还提供了为特定请求或会话在服务器上转换时区的方法:查看文档.但是,使用 JavaScript 绝对是更简单的选择.

Django also provides methods to convert timezones on the server for a particular request or a session: See docs. However, using JavaScript is definitely the easier option.

Django 还有一个 TIME_ZONE 设置.但这是非常有限的.Django 使用这个时区来显示管理员或其他地方的时间.它非常有限,因为只支持一个时区,并且不会根据客户端的时区而改变.

Django also has a TIME_ZONE setting. But this is very limited. Django uses this timezone for displaying the time in admin or other places. It's very limited because only one timezone is supported, and it won't change depending upon the client's timezone.

在用户浏览器上使用 JavaScript 将 UTC 转换为本地时间是最好和最简单的解决方案.

Converting UTC to localtime on a user's browser using JavaScript is the best and easiest solution.

注意:可以在此处找到此答案的旧版本(标记为已接受).

Note: The older version of this answer (which was marked accepted) can be found here.

这篇关于Django 会自动检测最终用户的时区吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆