From 94234b4294e58ce10e22344f22c67fa65dd008c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Fri, 3 Jun 2016 19:08:23 +0200 Subject: [PATCH] Order by category and add a header for each category. --- shop/templates/shop_index.html | 16 ++++++++++++---- shop/views.py | 4 ++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/shop/templates/shop_index.html b/shop/templates/shop_index.html index a7bf01cc..45df7340 100644 --- a/shop/templates/shop_index.html +++ b/shop/templates/shop_index.html @@ -27,22 +27,30 @@ Product - Category Price {% for product in products %} + + {% ifchanged product.category %} + + + + {{ product.category }} + + + + + {% endifchanged %} + {{ product.name }} - - {{ product.category }} -
{{ product.price|currency }} diff --git a/shop/views.py b/shop/views.py index d6c1e6ab..08cc0f03 100644 --- a/shop/views.py +++ b/shop/views.py @@ -141,6 +141,10 @@ class ShopIndexView(ListView): template_name = "shop_index.html" context_object_name = 'products' + def get_queryset(self): + queryset = super(ShopIndexView, self).get_queryset() + return queryset.order_by('category__name', 'price') + def get_context_data(self, **kwargs): context = super(ShopIndexView, self).get_context_data(**kwargs)