2011-08-10 36 views
0

我定義該數據結構:(GAE和django的)模板濾波器和時間格式顯示

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' object has no attribute 'm'

我試過其他的時間格式代碼,我發現,我只能顯示時間與「分」和「第二」。 與「m」,「M」,「n」,「N」和「F」等「月」或「日」相關的任何時間格式都會導致錯誤消息。

有人可以告訴我爲什麼?

我想顯示月,日,分和秒的創建時間。 我不想做這種方式:

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

我認爲這是羅嗦。

謝謝!併爲我可憐的英語感到抱歉。

回答

3

只需使用date過濾器即可。 From 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是月。

+0

我想我問一個愚蠢的問題:P 非常感謝!它真的幫助我! –

+0

那麼datetime過濾器在哪裏? –

+0

過濾器只是日期,也更新了答案https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#date –

相關問題