Grouping lists in a template

34 views
Skip to first unread message

Andrea

unread,
Jul 13, 2015, 12:50:11 AM7/13/15
to django...@googlegroups.com
Hello. I'm new to both django and python. Been trying to figure out how to group items from  a model for hours now. 

This in an example code i made up to illustrate my problem:
# Models
class Book(models.Model):
    submitted_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='book_subby')
    submitted_date = models.DateField(auto_now=True)
    book_title = models.CharField(max_length=200)
    book_publisher = models.ForeignKey(Publishers)

class Publishers(models.Model):
    submitted_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='publisher_subby')
    submitted_date = models.DateField(auto_now=True)
    book_publisher = models.CharField(max_length=200)

# View
def book_list(request):
    books = Book.objects.filter(submitted_by=request.user, submitted_date__lte=timezone.now()).order_by('submitted_date')

    return render(request, 'book/list_books.html', {
        'books': books 
        })

# Template list_books.html
{% for book in books %}
    <ul> {{ book.book_publisher }}
    <li> {{book.book_title }} </li>
{% endfor %}

This displays:

Publisher X
* Book A
Publisher Y
* Book B
Publisher X
* Book C
Publisher Y
* Book D

I want to display:
Publisher X
* Book A
* Book C
Publisher Y
* Book B
* Book D

I tried doing multiple for loops in the template, but couldn't make it work.
I would appreciate it alot if someone could nudge me in the right direction on how to solve this. 
Thanks, Andrea

Daniel Roseman

unread,
Jul 13, 2015, 10:10:29 AM7/13/15
to django...@googlegroups.com
There are several ways of doing this. One is to sort by Publisher first: 

    books = Book.objects.filter(...).select_related('book_publisher').order_by('book_publisher__book_publisher', 'submitted_date')

and use the {% ifchanged %} tag to only print the publisher name when it changes:

{% for book in books %}
    {% ifchanged %} {{ book.book_publisher }} {% endif changed %}
    <li> {{book.book_title }} </li>
{% endfor %}


--
DR.

Scot Hacker

unread,
Jul 13, 2015, 3:12:00 PM7/13/15
to django...@googlegroups.com


On Sunday, July 12, 2015 at 5:50:11 PM UTC-7, Andrea wrote:
Hello. I'm new to both django and python. Been trying to figure out how to group items from  a model for hours now. 

Check out the `regroup` template tag:


It can probably save you from writing custom code for this.

./s

anotherdjangonewby

unread,
Jul 13, 2015, 9:51:35 PM7/13/15
to django...@googlegroups.com

Another approach would be to build a list of dictionaries based on the query and pass this to the template instead of the pure llst of books:


[{

publisher: 'Publisher X',

books: ['Book A', 'Book B']

}],....


So the logic remains in the code and not in the template.


Kai

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/40de5ec2-c4d1-4cf2-8e4e-766dea21460b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages