Its me
Its me

Reputation: 1

How to pass .strftime() to groupby in jinja

I need to group items in list by it's date so I decided to use strftime as my tool but it just gives an error

<ul>{% for day, item in items|groupby("date_created.day") %}
    <li>{{ day }}
      <ul>{% for i in item %}
        <li>{{ i.product }}
      {% endfor %}</ul>
    </li>
  {% endfor %}</ul>

That's the output of code example above

But if I change .day to .strftime('%Y-%m-%d') it gives me an error

jinja2.exceptions.UndefinedError: 'datetime.datetime object' has no attribute "strftime('%Y-%m-%d')"

Upvotes: 0

Views: 125

Answers (1)

farialima
farialima

Reputation: 317

The group_by expression doesn't support arbitrary python expressions. You will need to compute an additional field (e.g. formatted_date_created), and add it to each of your items elements, in your view function (the one that calls the template)

Upvotes: 1

Related Questions