Django1.11自带分页器paginator如何使用
人气:0接下来我编写一个 views ,名classify。
classify,将模拟请求购物网站的分类产品、并分页返回数据。完成如下任务:
1、接受两个参数,type,page。”type”:为请求的购物网站产品类别,如 0代表热门。1代表 家居<
2、分类找到产品。分页返回。
为了方便将要传递的参数直接加载URL中。如:127.0.0.1:8000/classify/0/1/ 请求的是 “type” = 0 ,”page” = 1的数据。
URLS
from django.conf.urls import url from taobao import views urlpatterns = [ url(r'^classify/(\d+)/(\d+)/$',views.classify), ]
models
“category” :分类,0.代表热门;
class goods(models.Model): category = models.IntegerField('分类',default=0) goods_id = models.CharField('商品ID',max_length=10) goods_name = models.CharField('商品名',max_length=100,default='') goods_price = models.DecimalField('商品价格',max_digits=10,decimal_places=2) goods_Stock = models.IntegerField('商品库存',default=100) sales_Volume = models.IntegerField('销量',default=0) goods_introduce = models.CharField('商品简介',max_length=250,default='') def __str__(self): return self.goods_name
views
from taobao.models import goods from django.core.paginator import Paginator ,PageNotAnInteger ,EmptyPage def classify(req,type,page): # 接收从url中传递的两个参数。 context = {} context['type'] = int(type) if type == '0': goods_list = goods.objects.order_by('sales_Volume').all() # 按销量排序 else: goods_list = goods.objects.all().filter(category = int(type)).order_by('sales_Volume').all() paginator = Paginator(goods_list,8) # 把商品分成 8 个一页。 try: # 尝试获取请求的页数的 产品信息 goodss = paginator.page(int(page)) #请求页数错误 except PageNotAnInteger: goodss = paginator.page(1) except EmptyPage: goodss = paginator.page(paginator.num_pages) context['goods'] = goodss return render(req,'classify.html',context)
html页面 classify.html
{% block content %} <div style="margin-top: 20px"> <div class="w1240"> <div class="line100"></div> {# 根据type分类 显示不同图片汉字 #} <div class="icon"><img src="/static/images/type/{{ type }}.png"> <a>{% if type == 0 %}热门{% elif type == 1 %}美味餐厨{% elif type == 2 %}家纺家居{% endif %}</a> </div> </div> </div> <div class="w1240"> <ul class="m-itemList m-itemList-level2Category"> {% for i in goods %} <li class="item"> <div class="m-product"> <span></span> <div class="hd"> <a href="/taobao/goods/{{ i.goods_id }}" > <img src="/static/images/goods/{{ i.goods_id}}.jpg" class ="img"> </a> </div> <div class="bd"> <a href="/taobao/goods/{{ i.goods_id }}" > <h4 class="name">{{ i.goods_name }}</h4> </a> <p style="color: #050505;font-weight: bold;text-align:center" >{{ i.goods_introduce }}</p> <p class="price">¥{{ i.goods_price }}</p> <hr> </div> </div> </li> {% endfor %} </ul> <div style="padding-right:60px"> <span style="float: right"> <a href="/taobao/classify/{{ type }}/{{ goods.start_index }}" > 第一页 | </a> {% if goods.has_previous %} <a href="/taobao/classify/{{ type }}/{{ goods.previous_page_number }}" > 上一页 </a> {% if goods.has_next%} <span> | </span> {% endif %} {% endif %} {% if goods.has_next %} <a href="/taobao/classify/{{ type }}/{{ goods.next_page_number }}" > 下一页 </a> {% endif %} <a href="/taobao/classify/{{ type }}/{{ goods.end_index }}" >| 末尾页 </a> </span> </div> </div> <div class="clear"></div> {% endblock %}
您可能感兴趣的文章:
加载全部内容