【问题标题】:(GAE and django) template filter and time format display(GAE 和 django) 模板过滤和时间格式显示
【发布时间】:2011-10-23 17:30:30
【问题描述】:

我定义了这个数据结构:

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 }}

我觉得有点啰嗦。

谢谢!并为我糟糕的英语感到抱歉。

【问题讨论】:

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


    【解决方案1】:

    只需使用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.
    

    m 是月份。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 2016-01-11
    • 1970-01-01
    • 2017-11-15
    • 2013-08-17
    • 2015-11-22
    • 2012-07-01
    • 2023-03-16
    相关资源
    最近更新 更多