Chris
Chris

Reputation: 170

Python - Google App Engine - Methods in Django template

I'm coming from a Rails programming experience.

I'm coding an app in Python on Google App Engine with Django templates.

I can't call strftime("%I:%M %p") on a datetime.

In my main.py file I have:

import datetime
....
class Report(db.Model):
    date = db.DateTimeProperty(auto_now_add = True)

I pass all the reports to the template and in the template I have:

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

I know I have a datetime object because I can do:

{{ report.date.day }} 

or:

{{ report.date.minute }} 

Just fine and they return what they are supposed to return.

When I do have:

 {{ report.date.strftime("%I:%M %p") }}

I get the error:

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

How do I get strftime to work?

Upvotes: 2

Views: 692

Answers (2)

Camilo Martinez
Camilo Martinez

Reputation: 1803

The Django template system is "meant to express presentation, not program logic" that's why it "is not simply Python embedded into HTML".

Only tags and filters are supported by default. That's why you needed to find a filter similar to the function you pointed out, as @Dougal did. The other option would be to make a custom template tag or filters, but in this case would be an overkill.

Upvotes: 1

Danica
Danica

Reputation: 28846

The Django template engine doesn't support passing arguments to methods like that. To accomplish the same thing, try using the date filter:

{{ report.date|date:"f A" }}

(Edited: fixed the syntax for the date string, because I'm stupid.)

Upvotes: 5

Related Questions