2009-08-21 53 views

回答

1

在視圖代碼中使用排序列表,

sorted(dom_obj.objects.all(), key=lambda d: d.sort_key) 

,然後使用過濾器標籤

{% ifchanged %}<h1>{{ prod.cat }}</h1>{% endifchanged %} 
4

除了什麼@Wade建議你還可以添加一個方法到您的分類模型,返回它的產品。

例..

class Category: 
... 
... 
    def get_products(self): 
     return Product.objects.filter(category=self) 
在模板中,你可以

然後..

{% for category in categories %} # assuming categories is passed from the view. 
    {% for product in category.get_products %} 
...