具有系统时区设置的Django与用户的单个时区 [英] Django with system timezone setting vs user's individual timezones

查看:57
本文介绍了具有系统时区设置的Django与用户的单个时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django对每个用户处理不同时区的情况如何?理想情况下,我想在UTC时区(例如,在settings.py中设置TIME_ZONE = UTC)中运行服务器,以便所有日期时间都以UTC的形式存储在数据库中。像这样的东西使我感到恐惧,这就是为什么我到处都喜欢UTC的原因。 / p>

但是为每个用户存储时区并仍然使用标准django datetime格式和modelform包装器将有多困难。我是否期望必须到处编写日期处理代码才能将日期转换为用户的时区并再次返回UTC?



我仍在阅读django教程,但我知道如何在某些其他假设系统时区的框架中处理用户时区可能是一件很痛苦的事,所以我想我现在就问一下。



我目前的研究包括搜索django文档,仅找到一个参考






其他:




解决方案

更新,2013年1月:Django 1.4现在具有时区支持






出于历史原因的旧答案:



我将继续研究这个问题我自己为我的应用程序。解决这个问题的第一个方法是在中使用django核心开发人员Malcom Tredinnick的建议。此django用户的帖子。您可能希望将用户的时区设置存储在他们的用户个人资料中。



我也强烈建议您查看 pytz模块,这使使用时区的工作更加轻松。对于前端,我根据pytz中常见的时区创建了一个时区选择器。我为该区域提供了一个选择框,为位置提供了另一个选择框(例如,使用两个选择框呈现了美国/中央)。相较于浏览400多个选项,它使选择时区更加方便。


How well does Django handle the case of different timezones for each user? Ideally I would like to run the server in the UTC timezone (eg, in settings.py set TIME_ZONE="UTC") so all datetimes were stored in the database as UTC. Stuff like this scares me which is why I prefer UTC everywhere.

But how hard will it be to store a timezone for each user and still use the standard django datetime formatting and modelform wrappers. Do I anticipate having to write date handling code everywhere to convert dates into the user's timezone and back to UTC again?

I am still going through the django tutorial but I know how much of a pain it can be to deal with user timezones in some other frameworks that assume system timezone everywhere so I thought I'd ask now.

My research at the moment consisted of searching the django documentation and only finding one reference to timezones.


Additional:

解决方案

Update, January 2013: Django 1.4 now has time zone support!!


Old answer for historical reasons:

I'm going to be working on this problem myself for my application. My first approach to this problem would be to go with django core developer Malcom Tredinnick's advice in this django-user's post. You'll want to store the user's timezone setting in their user profile, probably.

I would also highly encourage you to look into the pytz module, which makes working with timezones less painful. For the front end, I created a "timezone picker" based on the common timezones in pytz. I have one select box for the area, and another for the location (e.g. US/Central is rendered with two select boxes). It makes picking timezones slightly more convenient than wading through a list of 400+ choices.

这篇关于具有系统时区设置的Django与用户的单个时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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