mirror of https://github.com/django/django.git
Changed the example in the {% regroup %} docstring.
This commit is contained in:
parent
606a303856
commit
47f57d6776
|
@ -1148,46 +1148,47 @@ def regroup(parser, token):
|
||||||
"""
|
"""
|
||||||
Regroups a list of alike objects by a common attribute.
|
Regroups a list of alike objects by a common attribute.
|
||||||
|
|
||||||
This complex tag is best illustrated by use of an example: say that
|
This complex tag is best illustrated by use of an example: say that
|
||||||
``people`` is a list of ``Person`` objects that have ``first_name``,
|
``musicians`` is a list of ``Musician`` objects that have ``name`` and
|
||||||
``last_name``, and ``gender`` attributes, and you'd like to display a list
|
``instrument`` attributes, and you'd like to display a list that
|
||||||
that looks like:
|
looks like:
|
||||||
|
|
||||||
* Male:
|
* Guitar:
|
||||||
* George Bush
|
* Django Reinhardt
|
||||||
* Bill Clinton
|
* Emily Remler
|
||||||
* Female:
|
* Piano:
|
||||||
* Margaret Thatcher
|
* Lovie Austin
|
||||||
* Colendeeza Rice
|
* Bud Powell
|
||||||
* Unknown:
|
* Trumpet:
|
||||||
* Pat Smith
|
* Duke Ellington
|
||||||
|
|
||||||
The following snippet of template code would accomplish this dubious task::
|
The following snippet of template code would accomplish this dubious task::
|
||||||
|
|
||||||
{% regroup people by gender as grouped %}
|
{% regroup musicians by instrument as grouped %}
|
||||||
<ul>
|
<ul>
|
||||||
{% for group in grouped %}
|
{% for group in grouped %}
|
||||||
<li>{{ group.grouper }}
|
<li>{{ group.grouper }}
|
||||||
<ul>
|
<ul>
|
||||||
{% for item in group.list %}
|
{% for musician in group.list %}
|
||||||
<li>{{ item }}</li>
|
<li>{{ musician.name }}</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
As you can see, ``{% regroup %}`` populates a variable with a list of
|
As you can see, ``{% regroup %}`` populates a variable with a list of
|
||||||
objects with ``grouper`` and ``list`` attributes. ``grouper`` contains the
|
objects with ``grouper`` and ``list`` attributes. ``grouper`` contains the
|
||||||
item that was grouped by; ``list`` contains the list of objects that share
|
item that was grouped by; ``list`` contains the list of objects that share
|
||||||
that ``grouper``. In this case, ``grouper`` would be ``Male``, ``Female``
|
that ``grouper``. In this case, ``grouper`` would be ``Guitar``, ``Piano``
|
||||||
and ``Unknown``, and ``list`` is the list of people with those genders.
|
and ``Trumpet``, and ``list`` is the list of musicians who play this
|
||||||
|
instrument.
|
||||||
|
|
||||||
Note that ``{% regroup %}`` does not work when the list to be grouped is not
|
Note that ``{% regroup %}`` does not work when the list to be grouped is not
|
||||||
sorted by the key you are grouping by! This means that if your list of
|
sorted by the key you are grouping by! This means that if your list of
|
||||||
people was not sorted by gender, you'd need to make sure it is sorted
|
musicians was not sorted by instrument, you'd need to make sure it is sorted
|
||||||
before using it, i.e.::
|
before using it, i.e.::
|
||||||
|
|
||||||
{% regroup people|dictsort:"gender" by gender as grouped %}
|
{% regroup musicians|dictsort:"instrument" by instrument as grouped %}
|
||||||
"""
|
"""
|
||||||
bits = token.split_contents()
|
bits = token.split_contents()
|
||||||
if len(bits) != 6:
|
if len(bits) != 6:
|
||||||
|
|
Loading…
Reference in New Issue