[Answer]-Rename grouper in regroup

1๐Ÿ‘

โœ…

A custom template tag should help you out here. It has to be put into a file e.g. my_custom_filters.py in your app in a folder called templatetags:

from django import template
register = template.Library()

def get_weekday(value):
    """returns the weekday for the given number - 0 indexed"""
    wd_list = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    return wd_list[int(value)]

This custom filter will be used like this:

{% load my_custom_filters %}

{% regroup day by weekdays as weekday_list %}

<ul>
{% for day in weekday_list %}
    <li>{{ day.grouper|get_weekday }}
    <ul>
        {% for item in day.list %}
          <li>{{ item.name }}: {{ item.population }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>

If you are using a date object could use the build-in date template tag to convert a date to the corresponding weekday:

{% regroup day by weekdays as weekday_list %}

<ul>
{% for day in weekday_list %}
    <li>{{ day.grouper|date:"w" }}
    <ul>
        {% for item in day.list %}
          <li>{{ item.name }}: {{ item.population }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>
๐Ÿ‘คThomas Kremmel

Leave a comment