python3之天天生鲜列表页排序order_by()和分页器Paginator

python

 url(r"^list/(?P<category_id>d*)/(?P<page_num>d*)$", views.ListGoods.as_view(), name="list"),

(?P<category_id>d*)/(?P<page_num>d*)  定义了   def get(self,request,category_id,page_num):   传参必须要用的 变量名

class ListGoods(View):

def get(self,request,category_id,page_num):

# 获取sort参数:如果用户不传,就是默认的排序规则

sort = request.GET.get("sort")

#商品种类

categorys = GoodsCategory.objects.all()

#得到某一种类 get

try:

category = GoodsCategory.objects.get(id=category_id)

except GoodsCategory.DoesNotExist: #DoesNot:不 Exist:存在

return redirect("/list/1/1")

# 查询该类别所有商品SKU信息:按照排序规则来查询

if sort == "price":

# 按照价格由低到高

skus = GoodsSKU.objects.filter(category=category).order_by("price")

elif sort == "hot":

# 按照销量由高到低

skus = GoodsSKU.objects.filter(category=category).order_by("-sales") #倒序

else:

# 默认 如果不传入.order_by(字段),会发出警告!! UnorderedObjectListWarning

skus = GoodsSKU.objects.filter(category=category).order_by("id")

# 无论用户是否传入或者传入其他的排序规则,我在这里都重置成"default"

sort = "default"

#新品推荐

new_skus = skus.order_by("-create_time")[:2]

# 创建分页器 对象 一页显示的数量

paginator = Paginator(skus,2)

page_num = int(page_num) #转换

try:

#获取 第几页的对象

page_skus = paginator.page(page_num)

except EmptyPage:

# except Exception:

page_skus = paginator.page(1)

#获得页数列表 一共几页 可遍历出来1,2,3...

page_list = paginator.page_range

context = {

"sort":sort,

"categorys": categorys,

"category": category,

"page_skus":page_skus,

"new_skus":new_skus,

"page_list":page_list,

}

# 购物车

cart_num = 0

# 自定义购物车函数

cart_num = cartnum(request, cart_num)

# 字典更新 /添加 购物车数量

context.update(cart_num=cart_num)

return render(request, "list.html", context)

html

<div class="sort_bar">

<a href="{% url "goods:list" category.id 1 %}?sort=default" {% if sort == "default" %}class="active"{% endif %}>默认</a>

<a href="{% url "goods:list" category.id 1 %}?sort=price" {% if sort == "price" %}class="active"{% endif %}>价格</a>

<a href="{% url "goods:list" category.id 1 %}?sort=hot" {% if sort == "hot" %}class="active"{% endif %}>人气</a>

</div>

{% if sort == "default" %}class="active"{% endif %}   active 在css里设定了样式

类似于这样:

 分页器

# 创建分页器 对象  一页显示2个商品

paginator = Paginator(skus,2)

page_num = int(page_num) #转换

try:

#获取 第几页的对象

page_skus = paginator.page(page_num)

except EmptyPage:

# except Exception:

page_skus = paginator.page(1)

#获得页数列表 一共几页 可遍历出来1,2,3...

page_list = paginator.page_range

<div class="pagenation">

<a href="{% url "goods:list" category.id 1 %}?sort={{ sort }}">首页</a>

{% if page_skus.has_previous %}

<!-- previous page number:之前的页码 -->

<a href="{% url "goods:list" category.id page_skus.previous_page_number %}?sort={{ sort }}">&lt;上一页</a>

{% endif %}

{% for page in page_list %}

<a href="{% url "goods:list" category.id page %}?sort={{ sort }}"

{% if page == page_skus.number %} class="active" {% endif %}>{{ page }}</a>

{% endfor %}

{% if page_skus.has_next %}

<a href="{% url "goods:list" category.id page_skus.next_page_number %}?sort={{ sort }}">下一页></a>

{% endif %}

</div>

以上是 python3之天天生鲜列表页排序order_by()和分页器Paginator 的全部内容, 来源链接: utcz.com/z/531122.html

回到顶部