Django分页操作-内置和自定义分页使用

内置分页

效果展示:

views.py

from django.core.paginator import Paginator
def fenye(request):


    current_page = request.GET.get('page')

    user_list=models.UserInfo.objects.all()

    paginator=Paginator(user_list,10)
    try:
        posts=paginator.get_page(current_page)
    except Exception as e:
        posts = paginator.get_page(1)


    return render(request, 'fenye.html', {'posts': posts})

fenye.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Django内置分页</title>
</head>
<body>
    <ul>
    {% for user in posts.object_list %}
        <li>{{ user.user }}</li>
    {% endfor %}
    </ul>

    <div>
        <a href="fenye.html?page=1">INDEX</a>
        {% if posts.has_previous %}
            <a href="fenye.html?page={{ posts.previous_page_number }}">PREV</a>
        {% endif %}
        {% for num in posts.paginator.page_range %}
            <a href="fenye.html?page={{ num }}"> {{ num }}</a>
        {% endfor %}
        {% if posts.has_next %}
            <a href="fenye.html?page={{ posts.next_page_number }}">NEXT</a>
        {% endif %}
        <a href="fenye.html?page={{ posts.paginator.num_pages }}">END</a>

    </div>
</body>
</html>

自定义分页

最终效果:

page.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义分页</title>
</head>
<body>
    <ul>
    {% for user in userinfo_list %}
        <li>{{ user.user }}</li>
    {% endfor %}
    {{ page_info.pager|safe }}
    </ul>
</body>
</html>

urls.py

from django.urls import path
from app import views

urlpatterns = [
    path('index/', views.index),
    path('page.html', views.page),
]

views.py

class PageInfo(object):

    def __init__(self, current_page, total, per_page,url,show_page=11):
        """
        :param current_page:展示页
        :param total:总条数
        :param per_page:每页显示数
        :param url:执行的地址
        :param show_page:页码显示数
        """
        try:
            self.current_page = int(current_page)
        except Exception as e:
            self.current_page = 1

        self.total = total
        self.per_page = per_page
        self.url = url
        self.show_page=show_page

        a, b = divmod(total, per_page)
        if b:
            a += 1
        self.all_page = a

    def start_page(self):
        return (self.current_page - 1) * self.per_page

    def end_page(self):
        return self.current_page * self.per_page

    def pager(self):
        page_list = []

        half=int((self.show_page-1)/2)

        # start_page=1
        # end_page=self.all_page+1

        if self.all_page<self.show_page:
            start_page=1
            end_page=self.all_page+1
        else:
            if self.current_page<=half:
                start_page=1
                end_page=self.show_page+1
            else:
                if self.current_page+half>self.all_page:
                    start_page=self.all_page-self.show_page+1
                    end_page=self.all_page+1
                else:
                    start_page=self.current_page-half
                    end_page=self.current_page+half+1

        home="<a style='display:inline-block;padding:5px;margin:5px;' href='%s?page=1'>首页</a>"%(self.url)
        page_list.append(home)

        if self.current_page<=1:
            prev="<a style='display:inline-block;padding:5px;margin:5px;' href='#'>上一页</a>"
        else:
            prev = "<a style='display:inline-block;padding:5px;margin:5px;' href='%s?page=%s'>上一页</a>" % (self.url,self.current_page - 1)
        page_list.append(prev)

        for i in range(start_page, end_page):
            if i == self.current_page:
                temp = "<a style='display:inline-block;padding:5px;margin:5px;background-color:blue;' href='%s?page=%s'>%s</a>" % (self.url,i,i)
            else:
                temp = "<a style='display:inline-block;padding:5px;margin:5px;' href='%s?page=%s'>%s</a>" % (self.url,i,i)
            page_list.append(temp)

        if self.current_page>=self.all_page:
            nex="<a style='display:inline-block;padding:5px;margin:5px;' href='#'>下一页</a>"
        else:
            nex = "<a style='display:inline-block;padding:5px;margin:5px;' href='%s?page=%s'>下一页</a>" % (self.url,self.current_page +1)
        page_list.append(nex)

        lst = "<a style='display:inline-block;padding:5px;margin:5px;' href='%s?page=%s'>末页</a>"%(self.url,self.all_page)
        page_list.append(lst)

        return ''.join(page_list)

def page(request):
    # 获取用户访问页
    current_page = request.GET.get('page')

    # 总条数
    total = models.UserInfo.objects.all().count()

    page_info = PageInfo(current_page, total,10,'page.html',8)

    userinfo_list = models.UserInfo.objects.all()[page_info.start_page(): page_info.end_page()]

    return render(request, 'page.html', {'userinfo_list': userinfo_list, 'page_info': page_info})

 

相关推荐

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术工厂 设计师:CSDN官方博客 返回首页
评论

打赏作者

呆马蓝

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值