封装分页代码:ListView封装代码实现快速分页功能


发布时间:2020-01-05 06:55    作者: Uncle Hui   已过去:6 月,1 周   阅读总量:274 已被赞:0


在利用django做项目的过程中,我们常常需要对数据进行分页处理,这里封装一个类视图,以后需要分页的地方,拷贝本代码就可以快速实现分页,提高我们的工作效率.

效果图:实现过程:

1. views.py :

from django.views.generic import ListView
from .models import New_category

 

class CategoryListView(ListView):
    model = New_category  #数据表,分页数据来源的表模型名
    template_name = 'news_category_list.html' #前段展示数据的html模板
    context_object_name = 'newsCategorys' #前段获取分页数据进行遍历的变量名
    paginate_by = 1 #按多少条数据分一页
    ordering = 'c_time' #按什么字段排序
    page_kwarg = 'p' #url上?后的截取字符,如:127.0.0.1:8000/news?p=1
    def get_context_data(self, **kwargs):
        context = super(CategoryListView, self).get_context_data(*kwargs)
        paginator = context.get('paginator')
        page_obj = context.get('page_obj')
        pagination_data = self.get_pagination_data(paginator, page_obj, 3)
        context.update(pagination_data)
        return context
    def get_pagination_data(self, paginator, page_obj, around_count=2):
        current_page = page_obj.number
        num_pages = paginator.num_pages
        left_has_more = False
        right_has_more = False
        if current_page <= around_count + 2:
            left_pages = range(1, current_page)
        else:
            left_has_more = True
            left_pages = range(current_page - around_count, current_page)
        if current_page >= num_pages - around_count - 1:
            right_pages = range(current_page + 1, num_pages + 1)
        else:
            right_has_more = True
            right_pages = range(current_page + 1, current_page + around_count + 1)
        #查询总条数
        count=New_category.objects.all().count() #表名(模型)
        return {
            'left_pages': left_pages,
            'right_pages': right_pages,
            'current_page': current_page,
            'left_has_more': left_has_more,
            'right_has_more': right_has_more,
            'num_pages': num_pages,
            'count':count,
        }

 2. URL.py  

path('category_list/',views.CategoryListView.as_view(),name='category_list'),  

3. news_category_list.html:  #要遍历这个newsCategorys,就是我们第一步里写的

{% for newsCategory in newsCategorys %}
   <tr>
       <td>{{ forloop.counter }}</td>
       <td>{{ newsCategory.name }}</td>
       <td>{{ newsCategory.ms }}</td>
       <td>{{ newsCategory.c_time}}</td>
{% endfor %}

4.将下面的分页代码放到自己需要的位置,里面代码不用修改

 {#    分页#}
      <div class="page">
          <div>
                {#上一页#}
                {% if page_obj.has_previous %}
                    <a class="prev" href="{% url 'news:category_list' %}?p={{ page_obj.previous_page_number }}"><<</a>
                {% else %}
                    <a class="prev" href="javascript:void(0);" style="cursor: not-allowed"><<</a>
                {% endif %}
                {% if left_has_more %}
                    <a class="prev" href="{% url 'news:category_list' %}?p=1">1</a>
                    <a class="prev" href="javascript:void(0);">...</a>
                {% endif %}
                {# 左边的页码 #}
                {% for left_page in left_pages %}
                    <a class="num" href="{% url 'news:category_list' %}?p={{ left_page }}">{{ left_page }}</a>
                {% endfor %}
                {# 当前的页码#}
                <span class="current">{{ current_page }}</span>
                {#<a href="{% url 'menu_manage:menu_list' %}?p={{ current_page }}" class="current">{{ current_page }}</a>#}
                {# 右边的页码 #}
                {% for right_page in right_pages %}
                    <a class="num" href="{% url 'news:category_list' %}?p={{ right_page }}">{{ right_page }}</a>
                {% endfor %}
                {% if right_has_more %}
                    <a class="num" href="javascript:void(0);">...</a>
                    <a class="num" href="{% url 'news:category_list' %}?p={{ num_pages }}">{{ num_pages }}</a>
                {% endif %}
                {#下一页#}
                {% if page_obj.has_next %}
                    <a class="next" href="{% url 'news:category_list' %}?p={{ page_obj.next_page_number }}">&gt;&gt;</a>
                {% else %}
                    <a class="disabled next" href="javascript:void(0);" style="cursor: not-allowed">&gt;&gt;</a>
                {% endif %}
            </div>
      </div>
{#分页结束#}

 以上就是全部实现过程!

点赞

0




登陆后方可评论