Ouput时区感知没有过滤器的django datetime字段 [英] Ouput timezone aware django datetime fields without filters

查看:153
本文介绍了Ouput时区感知没有过滤器的django datetime字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我升级到django 1.4,我想利用时区支持,我有几个datetime字段保存在postgres,并且它们被保存假设我的城市的时区,一旦我设置

  USE_TZ = True 

并将时区设置到我的城市,我的模板中的日期过滤器标签输出正确的小时(timezoned)

  {{concert.datetime | date:'f'}} 

问题是:我使用datetime构建我的网址,像这样:

  {%url event artist_slug = concert.slug_name hour = concert.datetime.hur%} 

那些时间并不正确,小时仍然在UTC,并且改变了我的链接,我不能做,它将失去所有的页面排名和大量的网站链接使用,是不可行的,更不用说,看起来很奇怪,url与一个广告不同的小时。
我试过这个:

  {%url event artist_slug = concert.slug_name hour = concert.datetime.hour | date :'H'%} 

没有成功,日期过滤器标签不被应用,异常升高。
我有一个非常大的代码库和大量的模板,有没有任何方法可以解决这个问题,而不用使用一个返回datetime分区的accesor?



谢谢。

解决方案

实际上Django文档规定: p>


即使您的网站只有一个时区可用,它仍然是
好​​的做法,以数据库的形式存储数据。一个主要原因
是夏令时(DST)。许多国家都有一个DST系统,
,秋天在春天向后推进时钟。如果
您在当地工作,那么当转换发生时,您可能每年遇到两次
的错误。 (pytz文档更详细地讨论了
这些问题。)这可能对您的
博客无关紧要,但如果您过度计算或欠帐您的客户
,这是一个问题每年一小时,每年两次。这个问题的解决方案是
在代码中使用UTC,并且只有在与
最终用户交互时才使用本地时间。


此外:


当启用时区支持时,Django使用时区感知的datetime对象。如果你的代码创建datetime对象,他们也应该知道。在这种模式下,上面的例子变成:




 从django导入datetime 
。 utils.timezone import utc

now = datetime.datetime.utcnow()。replace(tzinfo = utc)




模板中的时区感知输出当您启用时区支持时,
Django将感知的datetime对象转换为当前时区,当
在模板中呈现。这个行为非常像格式
本地化。


最后,没有猴子补丁任何东西:
https://docs.djangoproject.com/en/1.6/topics/i18n / timezones /#template-tags


Hi i upgraded to django 1.4 and i want to take advantage of the timezone support, i got a few datetime fields saved in postgres, and they were saved assuming the timezone of my city, once i set

USE_TZ = True

And set the timezone to my city the date filter tags in my templates output the correct hour(timezoned)

{{ concert.datetime|date:'f' }}

The problem is: i use the datetime to build my urls, like this:

{% url event artist_slug=concert.slug_name hour=concert.datetime.hour %}

And those are not correctly timezoned, the hour is still in UTC and that changes my links, something i can't do, it would lose all the page rank and lots of sites link to use, is not feasible, not to mention that it looks weird that the url has a different hour than the one advertised. I tried this:

{% url event artist_slug=concert.slug_name hour=concert.datetime.hour|date:'H' %}

Without success, the date filter tag is not applied and an exception is rised. I have a fairily large codebase and lots of templates, is there any way to fix this without using an accesor that returns the datetime timezoned?

Thank you.

解决方案

Actually Django Documentation states:

Even if your Web site is available in only one time zone, it’s still good practice to store data in UTC in your database. One main reason is Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions happen. (The pytz documentation discusses these issues in greater detail.) This probably doesn’t matter for your blog, but it’s a problem if you over-bill or under-bill your customers by one hour, twice a year, every year. The solution to this problem is to use UTC in the code and use local time only when interacting with end users.

Furthermore:

When time zone support is enabled, Django uses time-zone-aware datetime objects. If your code creates datetime objects, they should be aware too. In this mode, the example above becomes:

import datetime
from django.utils.timezone import utc

now = datetime.datetime.utcnow().replace(tzinfo=utc)

Time zone aware output in templates When you enable time zone support, Django converts aware datetime objects to the current time zone when they’re rendered in templates. This behaves very much like format localization.

And finally, without monkey patching anything: https://docs.djangoproject.com/en/1.6/topics/i18n/timezones/#template-tags

这篇关于Ouput时区感知没有过滤器的django datetime字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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