django 分页记要

一、在创建完项目、app 、以后在与 manage.py 同级的文件下 创建一个 utils 目录 (用于存储分页功能),在此目录下创建一个pager.py

django 分页记要_第1张图片

∆∆ : 代码示例: 

class PageInfo(object):
    def __init__(self,current_page,all_count,per_page,base_url,show_page=11):
        """

        :param current_page:
        :param all_count: 数据库总行数
        :param per_page:  每页显示 的函数
        :param base_url:
        :param show_page:

        """

        try:
            self.current_page=int(current_page)
        except Exception as e:
            self.current_page=1
        self.per_page=per_page

        a,b=divmod(all_count,per_page)

        if b:
            a=a+1
        self.all_pager=a
        self.show_page=show_page
        self.base_url=base_url

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

    def pager(self):
        page_list=[]

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

        if self.all_pager<self.show_page:
            begin=1
            stop=self.all_pager+1

        else:
            if self.current_page<=half:
                begin=1
                stop=self.show_page+1
            else:
                if self.current_page+half > self.all_pager:
                    begin=self.all_pager-self.show_page+1
                    stop=self.all_pager+1
                else:
                    begin=self.current_page-half
                    stop=self.current_page+half+1

        if self.current_page<=1:
            prev="
  • 上一页
  • " else: prev="
  • 上一页
  • "%(self.base_url,self.current_page-1,) page_list.append(prev) for i in range(begin,stop): if i==self.current_page: temp="
  • %s
  • "%(self.base_url,i,i) else: temp="
  • %s
  • "%(self.base_url,i,i,) page_list.append(temp) if self.current_page>=self.all_pager: nex="
  • 下一页
  • " else: nex="
  • 下一页
  • "%(self.base_url,self.current_page+1) page_list.append(nex) return ''.join(page_list)

    二、app下的views.py 文件 导入 pager.py,  并渲染到 html 页面:

    from django.shortcuts import render,redirect,HttpResponse
    from django.views import View
    from CBV import models
    from django.core.paginator import Paginator, Page,PageNotAnInteger,EmptyPage
    # Create your views here.
    
    class Login(View):
    
        '''
        FORM  表单只有GET POST 两种
        AJAX的提交方式
        默认都认为
        get  查
        put 更新
        post 创建
        delete 删除
    
        '''
        def dispatch(self, request, *args, **kwargs):
            print('before')
            obj=super(Login,self).dispatch(request,*args,**kwargs)
            print('after')
            return obj
    
    
    
        def get(self,request):
            print(2)
            return render(request,'login.html')
    
        def post(self,request):
            print(32323)
            print(request.POST.get('user'))
            result=models.UserInfo.objects.all()
            for obj in result:
                print(obj.name,obj.age,obj.ut_id)
            return render(request,'login.html')
    
    
    
    
    def index(request):
        #测试创建300条数据
        # for i in range(300):
        #     name='root'+str(i)
        #     models.UserInfo.objects.create(name=name,age=18,ut_id=1)
    
        current_page=request.GET.get('page')
        user_list=models.UserInfo.objects.all()
        paginator=Paginator(user_list,10)
            # current_page=int(current_page)
        try:
            posts=paginator.page(current_page)
        except PageNotAnInteger as e:
            posts = paginator.page(1)
    
        except EmptyPage as e:
            posts=paginator.page(1)
        # userlist=models.UserInfo.objects.all()
    
        # paginator = Paginator(userlist, 10)  # 每页显示10条
        # posts=paginator.page(current_page) # 当前显示第几页
        #posts  中有一个叫 object_list 是分页之后的数据
        #has_previous
        #next_page
        #pre_page
        #page_range
        return render(request,'index.html',{'posts':posts})
    
    
    # class PageInfo(object):
    #     def __init__(self,current_page,all_count,per_page,base_url,show_page=11):
    #         try:
    #             self.current_page = int(current_page) #数据库总行数
    #             # self.per_page = per_page #每页显示函数
    #         except Exception as e:
    #             self.current_page=1
    #
    #         self.per_page=per_page
    #
    #         a,b=divmod(all_count,per_page)
    #
    #         if b:
    #             a = a + 1
    #         self.all_pager=a
    #
    #         self.show_page=show_page
    #
    #         self.base_url=base_url
    #
    #     def start(self):
    #         return (self.current_page-1 ) * self.per_page
    #
    #     def end(self):
    #         return self.current_page * self.per_page
    #
    #     def pager(self):
    #
    #         page_list=[]
    #
    #         half=int((self.show_page-1)/2)
    #
    #         #如果数据总页数<11
    #         if self.all_pager
    #             begin=1
    #             stop = self.all_pager + 1
    #
    #             #如果数据总页数 >11
    #         else:
    #             #如果当前页 <=5,让其永远显示 1到11页
    #             if self.current_page <= half:
    #                 begin=1
    #                 stop=self.show_page+1
    #             else:
    #                 if self.current_page + half > self.all_pager:
    #
    #                     begin = self.all_pager - self.show_page + 1
    #
    #                     stop = self.all_pager + 1
    #
    #                 else:
    #                     begin=self.current_page - half
    #
    #                     stop=self.current_page + half + 1
    #
    #         if self.current_page <= 1:
    #
    #             prev = "
  • 上一页
  • "
    # # else: # # prev="
  • 上一页
  • "%(self.current_page-1,)
    # # page_list.append(prev) # # for i in range(begin,stop): # if i == self.current_page: # temp="
  • %s
  • "%(i,i,)
    # else: # temp = "
  • %s
  • "%(i,i,)
    # page_list.append(temp) # # #下一页 # if self.current_page >= self.all_pager: # nex="
  • 下一页
  • "
    # else: # nex="
  • 下 一页
  • "%(self.current_page+1,)
    # page_list.append(nex) # # # return ''.join(page_list) from utils.pager import PageInfo def custom(request): #表示用户当前想访问的页码: # current_page=request.GET.get('page') # print(current_page,'-----------') # current_page = int(current_page) #每页显示 数据的个数 # per_page = 10 # 假如 是第一页: 数据 是:0--10 # 第二页: 10:20 # 第三页 20:30 # start=(current_page-1)*per_page # end=current_page*per_page # user_list=models.UserInfo.objects.all()[start:end]#起始位置:结束位置 # return render(request,'custom.html',{'user_list':user_list}) #访问方式http://127.0.0.1:9000/custom.html?page=4 all_count=models.UserInfo.objects.all().count() page_info=PageInfo(request.GET.get('page'),all_count,10,'/custom.html',11) user_list=models.UserInfo.objects.all()[page_info.start():page_info.end()] return render(request,'custom.html',{'user_list':user_list,'page_info':page_info})

    三、templates 下的html页面 将结果拿到

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
        <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css" />
    head>
    <body>
        <h1>用户列表 h1>
            <ul>
                {% for row in user_list %}
                    <li>{{ row.name }}li>  #获取数据库内容。
                {% endfor %}
            ul>
    
        <nav aria-label="Page navigation"> 
            <ul class="pagination">
                {{ page_info.pager|safe }}   #拿到页码
            ul>
        nav>
    body>
    html>

    四、页面

    django 分页记要_第2张图片

     

    转载于:https://www.cnblogs.com/tonycloud/articles/7082155.html

    你可能感兴趣的:(python,数据库,javascript,ViewUI)