Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
47f57d67
Kaydet (Commit)
47f57d67
authored
Agu 26, 2016
tarafından
Baptiste Mispelon
Kaydeden (comit)
Tim Graham
Agu 26, 2016
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Changed the example in the {% regroup %} docstring.
üst
606a3038
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
22 deletions
+23
-22
defaulttags.py
django/template/defaulttags.py
+23
-22
No files found.
django/template/defaulttags.py
Dosyayı görüntüle @
47f57d67
...
...
@@ -1148,46 +1148,47 @@ def regroup(parser, token):
"""
Regroups a list of alike objects by a common attribute.
This complex tag is best illustrated by use of an example:
say that
``
people`` is a list of ``Person`` objects that have ``first_name``,
``
last_name``, and ``gender`` attributes, and you'd like to display a lis
t
that
looks like:
*
Male
:
*
George Bush
*
Bill Clinton
*
Female
:
*
Margaret Thatcher
*
Colendeeza Rice
*
Unknown
:
*
Pat Smith
This complex tag is best illustrated by use of an example: say that
``
musicians`` is a list of ``Musician`` objects that have ``name`` and
``
instrument`` attributes, and you'd like to display a list tha
t
looks like:
*
Guitar
:
*
Django Reinhardt
*
Emily Remler
*
Piano
:
*
Lovie Austin
*
Bud Powell
*
Trumpet
:
*
Duke Ellington
The following snippet of template code would accomplish this dubious task::
{
%
regroup
people by gender
as grouped
%
}
{
%
regroup
musicians by instrument
as grouped
%
}
<ul>
{
%
for group in grouped
%
}
<li>{{ group.grouper }}
<ul>
{
%
for
item
in group.list
%
}
<li>{{
item
}}</li>
{
%
for
musician
in group.list
%
}
<li>{{
musician.name
}}</li>
{
%
endfor
%
}
</ul>
{
%
endfor
%
}
</ul>
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
that ``grouper``. In this case, ``grouper`` would be ``Male``, ``Female``
and ``Unknown``, and ``list`` is the list of people with those genders.
that ``grouper``. In this case, ``grouper`` would be ``Guitar``, ``Piano``
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
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
sorted by the key you are grouping by! This means that if your list of
musicians was not sorted by instrument
, you'd need to make sure it is sorted
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
()
if
len
(
bits
)
!=
6
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment