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


Posted in Python onOctober 31, 2019

本文实例为大家分享了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">¥{{ 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 %}

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

Python 相关文章推荐
python中关于时间和日期函数的常用计算总结(time和datatime)
Mar 08 Python
python实现遍历文件夹修改文件后缀
Aug 28 Python
python实现旋转和水平翻转的方法
Oct 25 Python
Python实现定时自动关闭的tkinter窗口方法
Feb 16 Python
python启动应用程序和终止应用程序的方法
Jun 28 Python
详解Python3 pandas.merge用法
Sep 05 Python
python numpy中cumsum的用法详解
Oct 17 Python
Pytest框架之fixture的详细使用教程
Apr 07 Python
Python flask框架实现浏览器点击自定义跳转页面
Jun 04 Python
python简单实现插入排序实例代码
Dec 16 Python
python基于opencv实现人脸识别
Jan 04 Python
Python中 range | np.arange | np.linspace三者的区别
Mar 22 Python
python实现批量文件重命名
Oct 31 #Python
Python使用matplotlib 画矩形的三种方式分析
Oct 31 #Python
python实现操作文件(文件夹)
Oct 31 #Python
python用requests实现http请求代码实例
Oct 31 #Python
python zip()函数使用方法解析
Oct 31 #Python
python实现按首字母分类查找功能
Oct 31 #Python
利用python Selenium实现自动登陆京东签到领金币功能
Oct 31 #Python
You might like
php地址引用(php地址引用的效率问题)
2012/03/23 PHP
PHP的几个常用数字判断函数代码
2012/04/24 PHP
基于Laravel实现的用户动态模块开发
2017/09/21 PHP
PHP实现求连续子数组最大和问题2种解决方法
2017/12/26 PHP
ThinkPHP5.0框架使用build 自动生成模块操作示例
2019/04/11 PHP
JQ获取动态加载的图片大小的正确方法分享
2013/11/08 Javascript
基于jQuery实现响应式圆形图片轮播特效
2015/11/25 Javascript
javascript显示上周、上个月日期的处理方法
2016/02/03 Javascript
深入浅析JavaScript的API设计原则
2016/06/14 Javascript
jQuery页面弹出框实现文件上传
2017/02/09 Javascript
$.browser.msie 为空或不是对象问题的多种解决方法
2017/03/19 Javascript
windows下vue.js开发环境搭建教程
2017/03/20 Javascript
CodeMirror js代码加亮使用总结
2017/03/25 Javascript
vue watch自动检测数据变化实时渲染的方法
2018/01/16 Javascript
JS设计模式之策略模式概念与用法分析
2018/02/05 Javascript
[10:53]2018DOTA2国际邀请赛寻真——EG
2018/08/11 DOTA
[02:38]2018年度DOTA2最佳劣单位选手-完美盛典
2018/12/17 DOTA
Python中的fileinput模块的简单实用示例
2015/07/09 Python
python遍历目录的方法小结
2016/04/28 Python
python+opencv+caffe+摄像头做目标检测的实例代码
2018/08/03 Python
使用numba对Python运算加速的方法
2018/10/15 Python
pytorch使用Variable实现线性回归
2019/05/21 Python
python 协程 gevent原理与用法分析
2019/11/22 Python
如何用OpenCV -python3实现视频物体追踪
2019/12/04 Python
python Plotly绘图工具的简单使用
2020/03/03 Python
如何解决python多种版本冲突问题
2020/10/13 Python
全球烹饪课程的领先预订平台:Cookly
2020/01/28 全球购物
奢华时尚的创新平台:Baltini
2020/10/03 全球购物
C++面试题:关于链表和指针
2013/06/05 面试题
SQL语言面试题
2013/08/27 面试题
护理专业自我鉴定
2014/01/30 职场文书
挖掘机司机岗位职责
2014/02/12 职场文书
驾驶员培训方案
2014/05/01 职场文书
四风查摆剖析材料
2014/10/10 职场文书
四风问题自查自纠工作情况报告
2014/10/28 职场文书
MySQL锁机制
2021/04/05 MySQL