视图函数views.py
# 种类id 页码 排序方式 # restful api -> 请求一种资源 # /list?type_id=种类id&page=页码&sort=排序方式 # /list/种类id/页码/排序方式 # /list/种类id/页码?sort=排序方式 class ListView(View): '''列表页''' def get(self, request, type_id, page): '''显示列表页''' # 获取种类信息 try: type = GoodsType.objects.get(id=type_id) except GoodsType.DoesNotExist: # 种类不存在 return redirect(reverse('goods:index')) # 获取商品的分类信息 types = GoodsType.objects.all() # 获取排序的方式 # 获取分类商品的信息 # sort=default 按照默认id排序 # sort=price 按照商品价格排序 # sort=hot 按照商品销量排序 sort = request.GET.get('sort') if sort == 'price': skus = GoodsSKU.objects.filter(type=type).order_by('price') elif sort == 'hot': skus = GoodsSKU.objects.filter(type=type).order_by('-sales') else: sort = 'default' skus = GoodsSKU.objects.filter(type=type).order_by('-id') # 对数据进行分页 paginator = Paginator(skus, 1) # 获取第page页的内容 try: page = int(page) except Exception as e: page = 1 if page > paginator.num_pages: page = 1 # 获取第page页的Page实例对象 skus_page = paginator.page(page) # todo: 进行页码的控制,页面上最多显示5个页码 # 1.总页数小于5页,页面上显示所有页码 # 2.如果当前页是前3页,显示1-5页 # 3.如果当前页是后3页,显示后5页 # 4.其他情况,显示当前页的前2页,当前页,当前页的后2页 num_pages = paginator.num_pages if num_pages < 5: pages = range(1, num_pages+1) elif page <= 3: pages = range(1, 6) elif num_pages - page <= 2: pages = range(num_pages-4, num_pages+1) else: pages = range(page-2, page+3) # 获取新品信息 new_skus = GoodsSKU.objects.filter(type=type).order_by('-create_time')[:2] # 获取用户购物车中商品的数目 user = request.user cart_count = 0 if user.is_authenticated(): # 用户已登录 conn = get_redis_connection('default') cart_key = 'cart_%d' % user.id cart_count = conn.hlen(cart_key) # 组织模板上下文 context = {'type':type, 'types':types, 'skus_page':skus_page, 'new_skus':new_skus, 'cart_count':cart_count, 'pages':pages, 'sort':sort} # 使用模板 return render(request, 'list.html', context)
模板
{% extends 'base_detail_list.html' %} {% block title %}天天生鲜-商品列表{% endblock title %} {% block main_content %} <div class="breadcrumb"> <a href="#">全部分类</a> <span>></span> <a href="#">{{ type.name }}</a> </div> <div class="main_wrap clearfix"> <div class="l_wrap fl clearfix"> <div class="new_goods"> <h3>新品推荐</h3> <ul> {% for sku in new_skus %} <li> <a href="{% url 'goods:detail' sku.id %}"><img src="{{ sku.image.url }}"></a> <h4><a href="{% url 'goods:detail' sku.id %}">{{ sku.name }}</a></h4> <div class="prize">¥{{ sku.price }}</div> </li> {% endfor %} </ul> </div> </div> <div class="r_wrap fr clearfix"> <div class="sort_bar"> <a href="{% url 'goods:list' type.id 1 %}" {% if sort == 'default' %}class="active"{% endif %}>默认</a> <a href="{% url 'goods:list' type.id 1 %}?sort=price" {% if sort == 'price' %}class="active"{% endif %}>价格</a> <a href="{% url 'goods:list' type.id 1 %}?sort=hot" {% if sort == 'hot' %}class="active"{% endif %}>人气</a> </div> <ul class="goods_type_list clearfix"> {% for sku in skus_page %} <li> <a href="{% url 'goods:detail' sku.id %}"><img src="{{ sku.image.url }}"></a> <h4><a href="{% url 'goods:detail' sku.id %}">{{ sku.name }}</a></h4> <div class="operate"> <span class="prize">¥{{ sku.price }}</span> <span class="unit">{{ sku.price}}/{{ sku.unite }}</span> <a href="#" class="add_goods" title="加入购物车"></a> </div> </li> {% endfor %} </ul> <div class="pagenation"> {% if skus_page.has_previous %} <a href="{% url 'goods:list' type.id skus_page.previous_page_number %}?sort={{ sort }}"><上一页</a> {% endif %} {% for pindex in pages %} {% if pindex == skus_page.number %} <a href="{% url 'goods:list' type.id pindex %}?sort={{ sort }}" class="active">{{ pindex }}</a> {% else %} <a href="{% url 'goods:list' type.id pindex %}?sort={{ sort }}">{{ pindex }}</a> {% endif %} {% endfor %} {% if skus_page.has_next %} <a href="{% url 'goods:list' type.id skus_page.next_page_number %}?sort={{ sort }}">下一页></a> {% endif %} </div> </div> </div> {% endblock main_content %}