Django实现简单分页功能的方法详解

yipeiwu_com6年前Python基础

本文实例讲述了Django实现简单分页功能的方法。分享给大家供大家参考,具体如下:

使用django的第三方模块django-pure-pagination

安装模块:

pip install django-pure-pagination

将'pure_pagination'添加到settings.py文件中

INSTALLED_APPS = (
  ...
  'pure_pagination',
)

在view.py文件中

from django.shortcuts import render
rom .models import mymodel
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
def NewsList(request):
  all_news = mymodel.objects.all().order_by('-add_time')
  # 分页功能
  try:
    page = request.GET.get('page', 1)
  except PageNotAnInteger:
    page = 1
  p = Paginator(all_news, 3, request=request)
  news = p.page(page)
  return render(request, 'rdxw.html', {'all_news': news})

在template.py文件中调用view传递的参数'all_news'需要加上'.object_list'

{% extends 'base.html' %}
{% block content %}
<ul>
{% for new in all_news.object_list %}
  <li>{{new.content}}</li>
{% endblock %}
</ul>

实现翻页的部分:

<div class="pageturn">
  <ul class="pagelist">
    {% if all_news.has_previous %}
      <li class="long"><a href="?{{ all_news.previous_page_number.querystring }}" rel="external nofollow" >上一页</a></li>
    {% endif %}
    {% for page in all_news.pages %}
      {% if page %}
        {% ifequal page all_news.number %}
          <li class="active"><a href="?{{ page.querystring }}" rel="external nofollow" rel="external nofollow" >{{ page }}</a></li>
        {% else %}
          <li><a href="?{{ page.querystring }}" rel="external nofollow" rel="external nofollow" class="page">{{ page }}</a></li>
        {% endifequal %}
      {% else %}
        <li class="none"><a href="">...</a></li>
      {% endif %}
    {% endfor %}
    {% if all_news.has_next %}
      <li class="long"><a href="?{{ all_news.next_page_number.querystring }}" rel="external nofollow" >下一页</a></li>
    {% endif %}
  </ul>
</div>

样式较文档提供的简化了很多,方便使用。

.pageturn .pagelist {
  display: table-cell;
  vertical-align: middle;
  overflow: hidden;
}
.pageturn li {
  width: 30px;
  height: 30px;
  line-height: 30px;
  margin-left: 10px;
  float: left;
  text-align: center;
}
.pageturn li:first-child {
  margin-left: 0;
}
.pageturn li:hover a, .pageturn .active a {
  background: #717171;
  color: #fff;
  border-color: #eaeaea;
}
.pageturn a {
  border: 1px solid #eaeaea;
  display: block;
  height: 28px;
  color: #6c6c6c;
}
.pageturn .long {
  width: 100px;
}
.pageturn .none a {
  border: 0;
}
.pageright {
  float: right;
  width: auto;
  display: inline;
  clear: none;
  margin-top: 10px;
}

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

python实现微信远程控制电脑

python实现微信远程控制电脑

首先,我们要先看看微信远程控制电脑的原理是什么呢? 我们可以利用Python的标准库控制本机电脑,然后要实现远程的话,我们可以把电子邮件作为远程控制的渠道,我们用Python自动登录...

python实现视频读取和转化图片

1)视频读取 import cv2 cap = cv2.VideoCapture('E:\\Video\\20000105_224116.dav') #地址 while(True...

Python实现微信公众平台自定义菜单实例

首先先获取access_token,并保存与全局之中 def token(requset): url = 'https://api.weixin.qq.com/cgi-bin/...

用Python计算三角函数之atan()方法的使用

 atan()方法返回x的反正切值,以弧度表示。 Syntax 以下是atan()方法的语法: atan(x) 注意:此函数是无法直接访问的,所以我们需要导入math模块,然后...

python中字典按键或键值排序的实现代码

字典排序 在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的。因此,为了使统计得到的结果更方便查看需要进行排序。Python中字典的排序分为按“键”排序和...