Django1.11自带分页器paginator的使用方法

所属分类: 脚本专栏 / python 阅读数: 1402
收藏 0 赞 0 分享

本文实例为大家分享了Django1.11自带分页器Django的具体使用方法,供大家参考,具体内容如下

接下来我编写一个 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">&yen;{{ 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 }}" > 第一页 | &nbsp;</a>
  {% if goods.has_previous %}
  <a href="/taobao/classify/{{ type }}/{{ goods.previous_page_number }}" > 上一页 &nbsp;</a>
  {% if goods.has_next%}
  <span> | </span>
  {% endif %}
  {% endif %}
  {% if goods.has_next %}
  <a href="/taobao/classify/{{ type }}/{{ goods.next_page_number }}" > 下一页 &nbsp;</a>
  {% endif %}
  <a href="/taobao/classify/{{ type }}/{{ goods.end_index }}" >| 末尾页 </a>
 </span>
 </div>
 </div>
 <div class="clear"></div>

{% endblock %}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Python常见加密模块用法分析【MD5,sha,crypt模块】

这篇文章主要介绍了Python常见加密模块用法,结合实例形式较为详细的分析了MD5,sha与crypt模块加密的相关实现方法与操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python向日志输出中添加上下文信息

这篇文章主要介绍了Python向日志输出中添加上下文信息的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现的简单dns查询功能示例

这篇文章主要介绍了Python实现的简单dns查询功能,结合实例形式分析了Python基于socket模块的dns信息查询实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

利用Anaconda完美解决Python 2与python 3的共存问题

Anaconda 是 Python 的一个发行版,如果把 Python 比作 Linux,那么 Anancoda 就是 CentOS 或者 Ubuntu,下面这篇文章主要给大家介绍了利用Anaconda完美解决Python 2与python 3共存问题的相关资料,文中介绍的非常详
收藏 0 赞 0 分享

Python随机读取文件实现实例

这篇文章主要介绍了Python随机读取文件的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

用生成器来改写直接返回列表的函数方法

下面小编就为大家带来一篇用生成器来改写直接返回列表的函数方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python爬虫入门教程--快速理解HTTP协议(一)

http协议是互联网里面最重要,最基础的协议之一,我们的爬虫需要经常和http协议打交道。下面这篇文章主要给大家介绍了关于python爬虫入门之快速理解HTTP协议的相关资料,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

老生常谈Python进阶之装饰器

下面小编就为大家带来一篇老生常谈Python进阶之装饰器。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈Python基础之I/O模型

下面小编就为大家带来一篇浅谈Python基础之I/O模型。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python如何获取服务器硬件信息

这篇文章主要为大家详细介绍了python获取服务器硬件信息的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多