Regroups a list of alike objects by a common attribute. This complex tag is best illustrated by way of an example: say that list_link is a list of URLList represented by dictionaries containing "title" and "url" keys. This example also support users to expand by clicking on the category name.
{% regroup list_link by category as category_list %}
<ul>
{% for category, post_list in category_list %}
<li>
<a data-toggle="collapse" data-target="#categorycollapse-{{ category.id }}">{{ category }}</a>
<ul>
<div id="categorycollapse-{{ category.id }}" class="collapse">
{% for post in post_list %}
<li>
<b>{{ post.title }}</b>
<p><a hre="#">{{ post.url }}</a></p>
</li>
{% endfor %}
</div>
</ul>
</li>
{% endfor %}
</ul>
That's will display like this:
When you click on those category name, that will expanding a hierarchical list like this:
{% regroup %} does not order its input! Our example relies on the fact that the list_link list was ordered by category in the first place. If the list_link list did not order its members by category, the regrouping would naively display more than one group for a single category. To fix that we need to order the category field from app/models.py:
class AddUrl(models.Model):title = models.CharField(max_length=128, verbose_name=_('title:'))url = models.URLField(unique=True, verbose_name=_('URL:'))category = models.ForeignKey(Category, null=True, blank=True, verbose_name=_('category:'), on_delete=models.CASCADE)created_on = models.DateTimeField(auto_now_add=True)modified_on = models.DateTimeField(auto_now=True)class Meta:ordering = ['category']
More detail visit a official Django documentation here: https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#regroup
There are no posts related to this blog!