如何更改日期字段格式表示

2024-04-26 06:41:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在model DateField中有一个字段 所以当我在网页上呈现它的时候

12-30-2012

我需要

^{2}$

我试着用内部模板

{{ item.pub_date.strftime("%d.%m.%Y") }}

但是我收到了解析错误。在控制台上它工作得很好。 然后在模型中尝试了一种新的方法

get_pub_date(self):
    self.pub_date.strftime("%d.%m.%Y")

当我在tempalte中调用这个方法时,在解析模板时也会遇到同样的错误


Tags: 方法模型self模板网页getdatemodel
1条回答
网友
1楼 · 发布于 2024-04-26 06:41:27

使用date template filter

{{ item.pub_date|date:"d.m.Y" }}

另请注意:

When used without a format string:

^{pr2}$

...the formatting string defined in the DATE_FORMAT setting will be used, without applying any localization.

相关问题 更多 >

    热门问题