django custom paging

Purely written record can later be used.

pagination.py

from django.utils.safestring import mark_safe
class Pagination:
'''

'''
    def __init__(self,current_page,data_list,request,per_n_page=10):
        self.current_page = int(current_page)
        self.per_n_page = int(per_n_page)
        self.data_list = data_list
        self.request = request
        self.url = request.path
    @property
    def start(self):
        return self.current_page*self.per_n_page-self.per_n_page

    @property
    def end(self):
        return self.current_page*self.per_n_page

    @property
    def get_page_no(self):
        total_page_count = len(self.data_list)
        count,y = divmod(total_page_count,self.per_n_page)
        if y:
            count += 1
        page_no = []
        for i in range(1,count):
            page_no.append(i)
        return page_no

    def separate(self):
        total_page_count = len(self.data_list)
        count,y = divmod(total_page_count,self.per_n_page)
        if y:
            count += 1
        page_no = []
        for i in range(1,count):
            page_no.append(i)
        page_list=[]     #頁面列表
        start_index = self.current_page - 3
        end_index = self.current_page + 3 + 1
        if start_index <= 0 :
            start_index = 1
        if end_index > count:
            end_index = count + 1
        temp = "<li class='page-item'><a class='page-link' href='%s?p=1' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>" % (self.url)
        page_list.append(temp)
        for i in range(start_index,end_index):
            if i == self.current_page:
                temp = "<li class='page-item active'><a class='page-link' href='%s?p=%s'>%s<span class='sr-only'>(current)</span></a></li>" % (self.url,i,i)
                # temp = "<a class='page active' href='%s?p=%s'>%s</a>" %(self.url,i,i)
            else:
                temp = "<li class='page-item'><a class='page-link' href='%s?p=%s'>%s</a></li>" % (self.url,i,i)
                # temp = "<a class='page' href='%s?p=%s'>%s</a>" %(self.url,i,i)
            page_list.append(temp)
        temp = "<li class='page-item'><a class='page-link' href='%s?p=%s' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>" % (self.url,count)
        page_list.append(temp)
        page_str = " ".join(page_list)
        page_str= mark_safe(page_str)

        return page_str

views.py

current_page = request.GET.get("p",1)
    page = Pagination(current_page,customer_list,request.path,per_n_page=5)
    customer_list = customer_list[page.start:page.end]
    page_str = page.separate()
    return render(request,'index.html',{list':list,
                                                'page_str':page_str,
                                                'username':username
                                                })

templates

<tbody>
                {% for i in list %}
                <tr>
                  <td>{{ i.item }}</td>
                </tr>
                {% endfor %}
              </tbody>
            </table>

              <nav aria-label="Page navigation">
                <ul class="pagination float-left  mt-3">
                  {{ page_str }}
                </ul>
                <select id="items_per_page" class="float-left ml-3 mt-3">
                  <option value="10">10</option>
                  <option value="15">15</option>
                  <option value="25">25</option>
                  <option value="50">50</option>
                </select>
              </nav>

 

Guess you like

Origin www.cnblogs.com/jbite9057/p/11878090.html