Where is the product list page?
- When we go to the home page, click on the third level of the three-level category, and you will enter the product list page. Corresponding to all products.
- We only have cell phone data in our database, so click on cell phone by default
- The following is the analysis and realization of the business of the list page:
Breadcrumbs
It is to display the current position level by level.
- We know that the table that stores the three-level classification is a self-associated table, so go directly to the code
def get_breadcrumb(cat3):
# 面包屑导航,传递三级分类的对象
cat2 = cat3.parent
cat1 = cat2.parent # 通过外键,从三级分类找到对应的一级分类
breadcrumb = {
'cat1': {
'url': cat1.channels.all()[0],
# 这里其实GoodsCategory, 和 GoodsChannel 类是一对一 ,但是定义的是外键 Foreignkey,所以语法上属于一类查多类,就需要用到这些 .all()[0]
'name': cat1.name
},
'cat2': cat2,
'cat3': cat3
}
return breadcrumb
List page paging and sorting
- Sort :
- This is simple, (1) We sort by the time when the products are created by default
- (2) Sort by commodity price from low to high
- (3) Sort by commodity sales from low to high
- Only one parameter control is needed for realization :
- url/? sort=default , through the value passed by the sort parameter, judge which sort
- Pagination:
- Five pieces of data per page by default (because there is less data in the database)
- To achieve , we use a pager: official documents
according to this, the data we query out can be paged;
The example is too detailed, so I won't go to the source code.
sales rank
For all the product data in the current list page, we need to show users the top-selling products to help them choose;
- Request address /hot/(?P<category_id>\d+)/
- We only need the id value of this three-level classification
- With this id value, we can find the top few products sold and return their data.
skus = models.SKU.objects.filter(category_id=category_id, is_launched=True).order_by('-sales')[:2]
Okay, that's the end here, we won't go to the specific code implementation. It's too cumbersome, the ideas are here, you can't go to rote to read the code.
over!