Python - Google App Engine - Django模板中的方法

Chr*_*ris 2 python methods google-app-engine django-templates

我来自Rails编程经验.

我正在使用Django模板在Google App Engine上用Python编写应用程序.

我不能在日期时间调用strftime("%I:%M%p").

在我的main.py文件中,我有:

import datetime
....
class Report(db.Model):
    date = db.DateTimeProperty(auto_now_add = True)
Run Code Online (Sandbox Code Playgroud)

我将所有报告传递给模板,并在模板中传递:

{% for report in reports %}
<tr>
    <td>{{ report.date.strftime("%I:%M %p") }}</td>
</tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

我知道我有一个日期时间对象,因为我可以这样做:

{{ report.date.day }} 
Run Code Online (Sandbox Code Playgroud)

要么:

{{ report.date.minute }} 
Run Code Online (Sandbox Code Playgroud)

很好,他们返回他们应该返回的东西.

当我有:

 {{ report.date.strftime("%I:%M %p") }}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

raise TemplateSyntaxError, "Could not parse the remainder: %s" % token[upto:] TemplateSyntaxError: Could not parse the remainder: ("%I:%M %p")

我如何让strftime工作?

Dou*_*gal 5

Django模板引擎不支持将参数传递给类似的方法.为了实现同样的事情,请尝试使用date过滤器:

{{ report.date|date:"f A" }}
Run Code Online (Sandbox Code Playgroud)

(已编辑:修复了日期字符串的语法,因为我很愚蠢.)