django 1.11 自定义分页功能的实现

在WEB中很多时候是免不了要分页的,如果我们的数据太多了,分出来6000多页或者更多的时候,用django自带的分页功能就不好看了,所以我们来自定义一下。我是参考http://www.cnblogs.com/nulige/p/6558207.html这篇文章的,不过如果照着抄的话,会报错的,我修改了几个地方,实现效果如下图:




最后代码:

urls.py的代码就不写了

views.py(我把这分页包装了一下,便于移植):

class CustomPaginator(Paginator):
    def __init__(self, current_page, per_pager_num, *args, **kwargs):
        self.current_page = int(current_page)
        self.per_pager_num = int(per_pager_num)
        super(CustomPaginator, self).__init__(*args, **kwargs)

    def pager_num_range(self):
        if self.num_pages < self.per_pager_num:
            return range(1, self.num_pages + 1)

        half_part = int(self.per_pager_num / 2)
        if self.current_page <= half_part:
            return range(1, self.per_pager_num + 1)

        if (self.current_page + half_part) > self.num_pages:
            return range(self.num_pages - self.per_pager_num + 1, self.num_pages)
        return range((self.current_page - half_part), (self.current_page + half_part + 1))

def pagedivide(request, USER_LIST):
        try:
            current_page = request.GET.get('page')
            paginator = CustomPaginator(current_page, 11, USER_LIST, 10)
        except:
            current_page = 1
            paginator = CustomPaginator(current_page, 11, USER_LIST, 10)

        try:
            paginator = paginator.page(current_page)
        except PageNotAnInteger:
            paginator = paginator.page(1)
        except EmptyPage:
            paginator = paginator.page(paginator.num_pages)
        return paginator


html模板:

<tbody>
                                  {% for re in users.object_list %}
                                      {% if re.is_target %}
                                      <tr class="gradeX" style="width:200px; height:50px;">
         <td>{{ re.created_at }} td>
         <td>中国  {{ re.origin_id }} td>
                                          <td>{{ re.origin_url | slice:"50" }}....td>
                                          <td>{{ re.origin_cookie | slice:"50" }}...td>
         <td><span class="am-icon-close" style="color:crimson">span>td>
      tr>
                                  {% else %}
      <tr class="gradeX" style="width:200px; height:60px;">
         <td>{{ re.created_at }} td>
         <td>中国  {{ re.origin_id }} td>
                                          <td>{{ re.origin_url | slice:"50" }}....td>
                                          <td>{{ re.origin_cookie | slice:"50" }}...td>
         <td><span class="am-icon-check" style="color:greenyellow">span>td>
      tr>
                                  {% endif %}
                                  {% endfor %}
      
   tbody>
table>
                          <nav aria-label="Page navigation" style="margin-left: 1000px">
                                  <ul class="pagination">
                                      {% if users.has_previous %}
                                          <li>
                                          <a href="?page={{ users.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">«span>a>
                                          li>
                                      {% endif %}

                                     {% for pg in users.paginator.pager_num_range %}
                                      {% if pg == users.number %}
                                      <li class="active"><a href="?page={{ pg }}">{{ pg }}a>li>
                                      {% else %}
                                      <li><a href="?page={{ pg }}">{{ pg }}a>li>
                                      {% endif %}
                                      {% endfor %}
                                      {% if users.has_next %}
                                          <li>
                                              <a href="?page={{ users.next_page_number }}" aria-label="Next"><span aria-hidden="true">»span>a>li>
                                      {% endif %}
                                          <span style="font-size: 20px;margin-left: 30px">{{ users.number }} /{{ users.paginator.num_pages }}span>
                                  ul>
                                  nav>












你可能感兴趣的:(web)