Django分页

Posted sun96

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django分页相关的知识,希望对你有一定的参考价值。

技术分享图片
LIST=[]
for i in range(1001):
    LIST.append(i)

def user_list(request):

    current_page = request.GET.get(p,1)  # 获取当前页数
    current_page = int(current_page)

    per_page_count = 20  # 定义获取显示多少条数据
    start = (current_page-1)*per_page_count       # 获取显示开始数据
    end = (current_page)*per_page_count           # 获取显示结束数据

    data = LIST[start:end]            # 切片

    all_count = len(LIST)               # 获取列表的长度既总元素个数



    total_page,div = divmod(all_count,per_page_count)    # 根据元素获取页数  每十个元素为一页

    if div:
        total_page += 1                   # 有余数的情况下加一

    page_list = []                       # 定义一个页数列表



    # 当前页 current_page
    # 总页数 total_page
    # 每页显示10条数据  per_page_count
    # 页码让显示11条
    # if 总页数 <11
    # start_index = 0  end_index = 总页数
    # else:  如果 当前页 <= 6  start_index=1  end_index=11
    # 否则 start_index=当前页-5  end_index=当前页+5+1
    # 如果当前页 +5 >总页数  总页数当做最后一个值  end_index=total_page+1

    page_num = 11  # 显示多少页码
    if total_page < page_num:
        start_index = 1
        end_index = total_page+1

    else:
        if current_page <= (page_num+1)/2:
            start_index = 1
            end_index = page_num +1
        else:
            start_index = current_page - (page_num-1)/2
            end_index = current_page + (page_num+1)/2

            if (current_page+(page_num-1)/2) > total_page:

                end_index = total_page+1
                start_index = total_page - page_num + 1

    if current_page == 1:
        prev = "<a class=‘c1 active‘ href=‘javascript:void(0);‘>上一页</a>"  # 当上一页是第一页时什么都不干
    else:
        prev = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>上一页</a>"%(current_page-1)  #上一页

    page_list.append(prev)
    for i in range(int(start_index),int(end_index)):      # 循环页数列表
         if i == current_page:

            temp = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>%s</a>" %(i,i)  # 如果是当前页数加上样式
         else:
            temp = "<a class=‘c1‘ href=‘/user_list.html/?p=%s‘>%s</a>" %(i,i)

         page_list.append(temp)

    if current_page == total_page:
        next = "<a class=‘c1 active‘ href=‘javascript:void(0);‘>下一页</a>"
    else:
        next = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>下一页</a>" % (current_page + 1)  # 下一页
    page_list.append(next)

    jump = """
           <input type=‘text‘ /><a onclick=‘jumpTo(this,"/user_list.html/?p=");‘ id=‘i1‘>Go</a> 
           
           <script>
           
           function jumpTo(ths,base){
           
            var  val = ths.previousSibling.value;
            location.href = base+val;
           
                      }
                      
           </script>
           """
    page_list.append(jump)

    from django.utils.safestring import mark_safe

    page = "".join(page_list)
    page = mark_safe(page)

    return render(request,user_list.html,{li:data,page:page,})
views.py
技术分享图片
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .c1 {

            display: inline-block;
            background-color: mediumvioletred;
            padding: 5px;
            margin: 5px;

        }

        .active{
            background-color: #0e90d2;

        }

    </style>

</head>
<body>

{% include "tag.html" %}
{{ page }}


</body>
</html>
user_list.html
技术分享图片
<ul>
    {% for item in li %}
    <li>{{ item }}</li>
    {% endfor %}
</ul>
tag.html

 第二种类

新建文件夹,page_div.py 把公共函数放在里面

技术分享图片
class Page():
    def __init__(self,current_page,data_count,per_page_count=20, page_num=11):

        self.current_page=current_page
        self.per_page_count=per_page_count
        self.page_num=page_num
        self.data_count=data_count

    def start(self):
        return (self.current_page-1) * self.per_page_count

    def end(self):
        return (self.current_page)*self.per_page_count

    def total_page(self):

        total_page, div = divmod(self.data_count, self.per_page_count)  # 根据元素获取页数  每十个元素为一页

        if div:
            total_page += 1  # 有余数的情况下加一

        return total_page

    def page_div(self):
        page_list = []  # 定义一个页数列表

        # 当前页 current_page
        # 总页数 total_page
        # 每页显示10条数据  per_page_count
        # 页码让显示11条
        # if 总页数 <11
        # start_index = 0  end_index = 总页数
        # else:  如果 当前页 <= 6  start_index=1  end_index=11
        # 否则 start_index=当前页-5  end_index=当前页+5+1
        # 如果当前页 +5 >总页数  总页数当做最后一个值  end_index=total_page+1


        if self.total_page() < self.page_num:
            start_index = 1
            end_index = self.total_page() + 1

        else:
            if self.current_page <= (self.page_num + 1) / 2:
                start_index = 1
                end_index = self.page_num + 1
            else:
                start_index = self.current_page - (self.page_num - 1) / 2
                end_index = self.current_page + (self.page_num + 1) / 2

                if (self.current_page + (self.page_num - 1) / 2) > self.total_page():
                    end_index = self.total_page() + 1
                    start_index = self.total_page() - self.page_num + 1

        if self.current_page == 1:
            prev = "<a class=‘c1 active‘ href=‘javascript:void(0);‘>上一页</a>"  # 当上一页是第一页时什么都不干
        else:
            prev = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>上一页</a>" % (self.current_page - 1)  # 上一页

        page_list.append(prev)
        for i in range(int(start_index), int(end_index)):  # 循环页数列表
            if i == self.current_page:

                temp = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>[%s]</a>" % (i, i)  # 如果是当前页数加上样式
            else:
                temp = "<a class=‘c1‘ href=‘/user_list.html/?p=%s‘>[%s]</a>" % (i, i)

            page_list.append(temp)

        if self.current_page == self.total_page():
            next = "<a class=‘c1 active‘ href=‘javascript:void(0);‘>下一页</a>"
        else:
            next = "<a class=‘c1 active‘ href=‘/user_list.html/?p=%s‘>下一页</a>" % (self.current_page + 1)  # 下一页
        page_list.append(next)

        jump = """
                           <input type=‘text‘ /><a onclick=‘jumpTo(this,"/user_list.html/?p=");‘ id=‘i1‘>Go</a> 

                           <script>

                           function jumpTo(ths,base){

                            var  val = ths.previousSibling.value;
                            location.href = base+val;

                                      }

                           </script>
                           """
        page_list.append(jump)

        from django.utils.safestring import mark_safe

        page = "".join(page_list)
        page = mark_safe(page)

        return page
类方法
技术分享图片
from utils import page_div

LIST=[]
for i in range(1001):
    LIST.append(i)

def user_list(request):

    current_page = request.GET.get(p,1)  # 获取当前页数
    current_page = int(current_page)

    page_obj = page_div.Page(current_page,len(LIST))

    data = LIST[page_obj.start():page_obj.end()]            # 切片
    page=page_obj.page_div()
    return render(request,user_list.html,{li:data,page:page,})
views.py

 

以上是关于Django分页的主要内容,如果未能解决你的问题,请参考以下文章

django实现分页功能

Django REST框架分页的jQuery代码

modx - 当我在同上片段中使用“&documents =”参数时,分页不起作用

Django—分页器与form组件

在 Django 中为不同视图共享相同的分页代码

Django rest_framewok框架的基本组件