django的分页

一.分页器的函数封装

# 分页计算最小页和最大页
def page_list_return(total, current=1):
    ‘‘‘
    分页,返回本次分页的最小页数和最大页数列表
    :param total:
    :param current:
    :return:
    ‘‘‘
    min_page = current - 2 if current - 4 > 0 else 1
    max_page = current + 4 if current + 4 < total else total
    return list(range(min_page, max_page + 1))

# 分页器
def pages(post_objects, request):
    ‘‘‘
    分页公共函数,返回分页的对象元组
    :param post_objects:
    :param request:
    :return:
    ‘‘‘
    # 分页器
    paginator = Paginator(post_objects, 10)
    try:
        # 本页页码
        current_page = int(request.GET.get(‘page‘, ‘1‘))
    except ValueError:
        current_page = 1
    # 所有页码
    page_range = page_list_return(len(paginator.page_range), current_page)
    try:
        # 本页对象
        page_objects = paginator.page(current_page)
    except (EmptyPage, InvalidPage):
        page_objects = paginator.page(paginator.num_pages)

    if current_page >= 5:
        show_first = 1
    else:
        show_first = 0
    if current_page <= (len(paginator.page_range) - 3):
        show_end = 1
    else:
        show_end = 0
    # 所有对象,分页器,本页对象,所有页码,本业页码,是否显示第一页,是否显示最后一页
    return post_objects, paginator, page_objects, page_range, current_page, show_first, show_end

二.view页面的传递

def menu_list(request):
    header_title, path = ‘菜单详情‘, ‘菜单详情‘
    menus_list = MenuInfo.objects.all()
    menus_list, p, page_obj, page_range, current_page, show_first, show_end = pages(menus, request)
    return render(request, ‘menu/menu_list.html‘,locals())

三.前端页面的代码

<div class="col-sm-7">
    <div class="dataTables_paginate paging_simple_numbers" id="editable_paginate">
        <ul class="pagination">
            {% if page_obj.has_previous %}
                <li class="paginate_button previous " id="editable_previous">
                    <a class="page" href="?page={{ page_obj.previous_page_number }}" aria-controls="editable"
                       data-dt-idx="0"
                       tabindex="0">上一页</a>
                </li>

            {% else %}
                <li class="paginate_button previous disabled" id="editable_previous">
                    <a class="page"  aria-controls="example1" data-dt-idx="0" tabindex="0">上一页</a>
                </li>
            {% endif %}
            {% ifequal show_first 1 %}
                <li class="paginate_button">
                    <a class="page" href="?page=1" aria-controls="editable" data-dt-idx="1" tabindex="0">1..</a>
                </li>
            {% endifequal %}

            {% for page in page_range %}
                {% ifequal current_page page %}
                    <li class="paginate_button active">
                        <a class="page" href="?page={{ page }}" aria-controls="editable" data-dt-idx="1" tabindex="0"
                           title="第{{ page }}页">{{ page }}</a>
                    </li>
                {% else %}
                    <li class="paginate_button">
                        <a class="page" href="?page={{ page }}" aria-controls="editable" data-dt-idx="1" tabindex="0"
                           title="第{{ page }}页">{{ page }}</a>
                    </li>
                {% endifequal %}

            {% endfor %}
            {% ifequal show_end 1 %}
                <li class="paginate_button">
                    <a class="page" href="?page={{ p.num_pages }}" aria-controls="editable" data-dt-idx="1" tabindex="0"
                       title="第{{ page }}页">...{{ p.num_pages }}</a>
                </li>
            {% endifequal %}
            {% if page_obj.has_next%}
                <li class="paginate_button next " id="editable_next">
                    <a class="page" href="?page={{ page_obj.next_page_number }}" aria-controls="editable"
                       data-dt-idx="1"
                       tabindex="0">下一页</a>
                </li>
            {% else %}
                <li class="paginate_button next disabled " id="editable_next">
                    <a class="page" aria-controls="editable"
                       data-dt-idx="1"
                       tabindex="0">下一页</a>
                </li>
            {% endif %}

        </ul>
    </div>
</div>

  

时间: 2024-10-18 00:03:06

django的分页的相关文章

django 自定义分页模块

django 自定义分页模块 from django.shortcuts import render, HttpResponse, redirect from django.utils.safestring import mark_safe class Page(object): def __init__(self, current_page): self.current_page = int(current_page) @property def start(self): return (se

django orm 分页(paginator)取数据出现警告manage.py:1: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: &lt;class &#39;sign.models.Guest&#39;&gt; QuerySet.

使用django的orm做分页(Paginator)时出现了下面的警告 In [19]: p=Paginator(guest_list,2) manage.py:1: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'sign.models.Guest'> QuerySet. #!/usr/bin/env python 网上搜的

Django html 分页

16.分页 django 自带的分页:django paginator 参考:https://docs.djangoproject.com/en/1.10/topics/pagination/ >>> from django.core.paginator import Paginator >>> objects = ['john', 'paul', 'george', 'ringo'] >>> p = Paginator(objects, 2) >

Django - Ajax分页

目前总结了2种方法: 1. Ajax 分页 尼玛各种google,stackoverflow,搞了好久才总结出这个,之前使用Pagination tag loading的方式不好用,并且不能进行ajax提交请求的页面无刷新的方式去分页 1.view.py 1 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 2 from django.shortcuts import render 3 def xxx

django之分页

分页 在当前网页如果特别的数据,如果不想当前网页显示太多的内容数据(例如一些文章的目录或者一些小说的章节目录很多地方都用到分页),这时候就需要用分页了. 在Django中数据被分在不同页面中,并带有“上一页/下一页”标签.这些类位于django/core/paginator.py中. 分页器Paginator Paginator.py源码 import collections from math import ceil from django.utils import six from djan

django的分页--不全也未实现

一.Django内置分页 Paginator 二.自定义分页 分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置. 1.设定每页显示数据条数 2.用户输入页码(第一页.第二页...) 3.根据设定的每页显示条数和当前页码,计算出需要取数据表的起始位置 4.在数据表中根据起始位置取值,页面上输出数据 需求又来了,需要在页面上显示分页的页面.如:[上一页][1][2][3][4][5][下一页] 1.设定每页显示数据条数 2.用户输入页码(第一页.第二

django之分页插件

1 from django.utils.safestring import mark_safe 2 3 4 class Page: 5 def __init__(self, current_page, data_count, per_page_count=2, pager_num=7): 6 self.current_page = current_page 7 self.data_count = data_count 8 self.per_page_count = per_page_count

django 实现分页功能

分页效果: 视图代码: 1 # -*- coding: utf-8 -*- 2 from django.shortcuts import render,get_object_or_404 3 from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage 4 5 from .models import Article 6 7 # Create your views here. 8 9 def index(request

django之分页、cookie装饰器

一.分页代码如下 from django.utils.safestring import mark_safe class Page: def __init__(self, current_page, data_count, per_page_count=10, pager_num=7): self.current_page = current_page self.data_count = data_count self.per_page_count = per_page_count self.p

Django自定义分页、bottle

一.使用django实现之定义分页 1.自定义分页在django模板语言中,通过a标签实现; 2.前段a标签使用<a href="/user_list/?page=1">1</a>,将page的值传送到函数/user_list/中,后端在user_list中通过request.GET.get('page',1)获取当前页; 3.从数据库中获取特定行的数据,使用result = models.UserList.objects.all()[start:end]获取,