(GAE和django)模板过滤器和时间格式显示

时间:2011-08-10 02:25:35

标签: django google-app-engine django-templates django-views

我定义了这个数据结构:

class Foo(db.Model):
  created = db.DateTimeProperty(required = True)

在视图中,我用以下内容显示实例的创建时间:

<p>{{ foo.created }}</p>

它完美无缺:

2011-08-09 15:11:30.437000

然后我用模板过滤器设置时间格式:

<p>{{ foo.created|time:"P" }}</p>

我得到了这个:

3:11 p.m.

它仍然是完美的。但后来我设置了这样的时间格式:

<p>{{ foo.created|time:"m/d H:i" }}</p>

然后我收到错误消息:

  

AttributeError:'TimeFormat'对象没有属性'm'

我尝试了其他时间的格式代码,我发现只能用“分钟”和“秒”显示时间。 任何与“月”或“日”相关的格式如“m”,“M”,“n”,“N”和“F”都会导致错误信息。

有人可以告诉我为什么?

我想显示月,日,分和秒的创建时间。 而且我不想这样做:

{{ foo.created.date.month }}/{{ foo.created.date.day }} {{ foo.created.time.minute }}:{{ foo.created.time.second }}

我认为这很罗嗦。

谢谢!抱歉我的英语不好。

1 个答案:

答案 0 :(得分:3)

只需使用date过滤器即可。来自the docs

The time filter will only accept parameters in the format string
that relate to the time  of day, not the date (for obvious reasons).
If you need to format a date, use the date filter.
我是月。