django 表單分頁

來源:互聯網
上載者:User
關鍵字 django python php html c
                    {% ifnotequal cur_page 1 %}                        
  • |<
  • <
  • {% else %}
  • |<
  • <
  • {% endifnotequal %}
  • {{ cur_page }}
  • {% ifnotequal cur_page total_page %}
  • {{ cur_page|add:1 }}
  • {{ cur_page|add:2 }}
  • {% endifnotequal %} {% if next %}
  • >
  • {% else %}
  • >
  • {% endif %} {% ifnotequal cur_page total_page %}
  • >|
  • {% else %}
  • >|
  • {% endifnotequal %}

    點擊到最後一頁,效果如下:

    但實際上只有9頁,判斷next(即當前頁是否存在下一頁)標籤時 加 3 溢出 到10頁。
    1、要求顯示的頁碼範圍數為3,不足的為2或者1;
    請教該如何處理?

    回複內容:

                        {% ifnotequal cur_page 1 %}                        
  • |<
  • <
  • {% else %}
  • |<
  • <
  • {% endifnotequal %}
  • {{ cur_page }}
  • {% ifnotequal cur_page total_page %}
  • {{ cur_page|add:1 }}
  • {{ cur_page|add:2 }}
  • {% endifnotequal %} {% if next %}
  • >
  • {% else %}
  • >
  • {% endif %} {% ifnotequal cur_page total_page %}
  • >|
  • {% else %}
  • >|
  • {% endifnotequal %}

    點擊到最後一頁,效果如下:

    但實際上只有9頁,判斷next(即當前頁是否存在下一頁)標籤時 加 3 溢出 到10頁。
    1、要求顯示的頁碼範圍數為3,不足的為2或者1;
    請教該如何處理?

    在Paginator上再封裝一層Spagination ,Views調用

    obj = Model.object.filter(條件)pagin = Spagination.wraper(obj, pageSize, page)return pagin

    Spagination類代碼

    #coding=utf-8from django.core.paginator import Paginatorclass Spagination(object):    def __init__(self, total, page=1, perpage=10):        self.total = total        self.pages = self.total / perpage + 1 if (self.total % perpage) > 0 else self.total / perpage        self.page = page        self.perpage = perpage        self.prev_num = page-1 if page > 1 else None        self.next_num = page+1 if page*perpage < total else None        self.has_prev = True if self.prev_num else False        self.has_next = True if self.next_num else False        self.visible_pages = self.iter_pages()    def prev(self):        if self.prev_num:            return Spagination(self.total, page=self.page-1, perpage=self.perpage)        raise 'There is no prev page!'    def next(self):        if self.next_num:            return Spagination(self.total, page=self.page+1, perpage=self.perpage)        raise 'There is no next page!'    def iter_pages(self, left_edge=5, right_edge=4, all=9):        left_list, right_list = [], []        for p in range(1,left_edge):            if self.page - p > 0:                left_list.insert(0, self.page - p)        for p in range(1,right_edge):            if self.page + p <= self.pages:                right_list.append(self.page + p)        if len(left_list) < left_edge and right_list:            for i in range(all-len(left_list)-len(right_list)-1):                if right_list[-1]+1 <= self.pages:                    right_list.append(right_list[-1]+1)        if len(right_list) < right_edge and left_list:            for i in range(all-len(left_list)-len(right_list)-1):                if left_list[0]-1 > 0:                    left_list.insert(0, left_list[0]-1)        return left_list+[self.page]+right_list    def setItems(self, items):        self.items = items    @classmethod    def wraper(cls, objs, limit, current_page):        paginator = Paginator(objs, limit)        items = paginator.page(current_page)        pagin = Spagination(paginator.count, current_page, limit)        pagin.setItems(items)        return pagin

    template代碼

        {%if pagin.has_prev %}        上頁    {%endif%}    首頁    {%for p in pagin.visible_pages%}        {%if p != 1 and p != pagin.pages%}            {{p}}        {%endif%}    {%endfor%}    尾頁    {%if pagin.has_next %}        下頁    {%endif%}

    可以再加一層判斷,判斷是否超出

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.