在django模板中渲染datetime对象时出错 [英] Error with rendering datetime object in django templates

查看:150
本文介绍了在django模板中渲染datetime对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于博客应用程序,我正在尝试获取所有月份的列表,其中发布了哪些帖子,但是在模板中找到了一组错误的月份。我被黑客攻击,发现以下结果,

For a blogging app, I'm trying to get a list of all months, in which posts have been published, but find a wrong set of months rendered in the template. I hacked around and found the following results,

>>> date_list = Entry.objects.filter().dates('pub_date','month') 

以上代码,给我一套正确的月份,

Above code, gives me a set of months which is correct,

>>> date_list[6]
 datetime.datetime(2012, 6, 1, 0, 0, tzinfo=<UTC>)

月份表示6月份,但是当用模板呈现时,该月份减少1即可能

The month indicates June, but when rendered with a template, the month is decreased by one i.e 'may'

>>> t = Template("The month is {{ month|date:'m' }}  ")
>>> c = Context({"month": date_list[6]})
>>> t.render(c)
 u'The month is 05  '

而实际价值是

 >>> date_list[6].month
   6

我错过了一些基本的东西,还是django模板引擎本身的错误?

Everything runs fine except this bug with months, Am I missing out something basic or is it a bug with django's template engine itself?

推荐答案

你的时区是什么?日期是UTC,所以2012-06-01 00:00:00在UTC时间可能还是前一天在另一个时区,说2012-05-31 20:00:00在东部。

What's your time zone? The date is in UTC, so 2012-06-01 00:00:00 in UTC time can be still a previous day in another time zone, say 2012-05-31 20:00:00 in Eastern.

以下是相关 docs

这篇关于在django模板中渲染datetime对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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