廖雪峰Python web实例_分页补充

廖的教程写的非常详细了,分页功能不完整。

后端代码:

apis.py

class Page(object):
    '''
    Page object for display pages.
    '''

    def __init__(self, item_count, page_index=1, page_size=10):
        '''
        Init Pagination by item_count, page_index and page_size.

        >>> p1 = Page(100, 1)
        >>> p1.page_count
        10
        >>> p1.offset
        0
        >>> p1.limit
        10
        >>> p2 = Page(90, 9, 10)
        >>> p2.page_count
        9
        >>> p2.offset
        80
        >>> p2.limit
        10
        >>> p3 = Page(91, 10, 10)
        >>> p3.page_count
        10
        >>> p3.offset
        90
        >>> p3.limit
        10
        '''
        self.item_count = item_count  # 数据总条数
        self.page_size = page_size  # 每页显示条数
        self.page_count = item_count // page_size + (1 if item_count % page_size > 0 else 0)  # item_count除以10+取余;总页数
        if (item_count == 0) or (page_index > self.page_count):  # 当表数据为0或者page_count<1;条数为0或页数为1
            self.offset = 0
            self.limit = 0
            self.page_index = 1
        else:
            self.page_index = page_index  # 当前页数
            self.offset = self.page_size * (page_index - 1)  # 0,10,20,30...
            self.limit = self.page_size  # 1,2,3,4...
        self.has_next = self.page_index < self.page_count  # 如果当前页数<总页数则下一页条件成立
        self.has_previous = self.page_index > 1  # 如果当前页大于1,则上一页条件成立
        self.page_default = 11  # 默认显示分页11页
        # 如果总页数<=11
        if self.page_count <= self.page_default:
            self.page_start = 1
            self.page_end = self.page_count + 1
            # self.page_range = range(1, self.page_count + 1)  # 遍历所有页数
        # 如果总页数>11
        else:
            # 如果当前页<=5
            if self.page_index <= int((self.page_default - 1) / 2):
                self.page_start=1
                self.page_end=self.page_default + 1
                # self.page_range = range(1, self.page_default + 1)
            else:
                # 如果当前页+5>总页数
                if (self.page_index + 5) > self.page_count:
                    self.page_start = self.page_count - self.page_default + 1
                    self.page_end = self.page_count + 1
                    # self.page_range = range(self.page_count - self.page_default + 1, self.page_count + 1)
                else:
                    self.page_start = self.page_index - int((self.page_default - 1) / 2)
                    self.page_end = self.page_index + int((self.page_default - 1) / 2) + 1
                    # self.page_range = range(self.page_index - int((self.page_default - 1) / 2),
                    #                         self.page_index + int((self.page_default - 1) / 2) + 1)

    def __str__(self):
        return 'item_count: %s, page_count: %s, page_index: %s, page_size: %s, offset: %s, limit: %s' % (
            self.item_count, self.page_count, self.page_index, self.page_size, self.offset, self.limit)

    __repr__ = __str__
 
  
前端代码:

__base__.html

{% macro pagination(url, page) %}
    
    {% if page.has_previous %}
  • {% else %}
  • {% endif %} {% for i in range(page_start,page_end) %} {% if i == page.page_index %}
  • {{ i }}
  • {% else %}
  • {{ i }}
  • {% endif %} {% endfor %} {% if page.has_next %}
  • {% else %}
  • {% endif %}
{% endmacro %}


apis.py中如果用page_range会报错 

AttributeError: range object has no Attribute '__dict__'

直接在jinjia2前端中使用range即可;


参考链接

https://www.cnblogs.com/haiyan123/p/8228447.html

我的博客:http://118.24.159.163/

你可能感兴趣的:(Python,Mysql,分页,前端)