Django简单博客实战(四)--- 分页实现

Django实现分页

项目地址:https://github.com/ylpxzx/lifeblog

Django自带分页组件实现分页器

  1. 视图函数views.py中配置
from django.shortcuts import render,HttpResponse
from django.views.generic import View
from .models import Post
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger # 添加该行
class IndexView(View):
    def get(self,request):
        post_list = Post.objects.all().order_by("-id")
        
        # 分页器
        paginator = Paginator(post_list,6)
        
        # print("count:",paginator.count)  # 数据总数
        # print("num_pages:",paginator.num_pages) # 总页数
        # print("page_range:",paginator.page_range) # 页码列表
        #
        # page1 = paginator.page(1)  # 第1页的page对象
        # for i in page1: # 遍历第1页的所有数据对象
        #     print(i)
        #
        # print("第一页的所有数据:",page1.object_list)  # 第1页的所有数据
        #
        # page2 = paginator.page(2)
        # print(page2.has_next())  # 是否有下一页
        # print(page2.next_page_number())  # 下一页的页码
        # print(page2.has_previous())  # 是否有上一页
        # print(page2.previous_page_number())  # 上一页的页码

        page = request.GET.get('page', 1)
        currentPage = int(page)

        try:
            print(page)
            post_list = paginator.page(page)
        except PageNotAnInteger:
            post_list = paginator.page(1)
        except EmptyPage:
            post_list = paginator.page(paginator.num_pages)


        return render(request,"index.html",{'post_list':post_list,"paginator":paginator,"currentPage":currentPage})
  1. 模板文件中编写分页语句
{% block area %}
<div class="col-lg-8">
    <div class="blog_left_sidebar">
    {% for post in post_list %}
        <li>{{ post }}</li>
    {% endfor %}
        
        <!-- 分页栏 -->
        <nav class="blog-pagination justify-content-center d-flex">
            <ul class="pagination">
                {% if post_list.has_previous %}
                <li class="page-item">
                    <a href="/index/?page={{ post_list.previous_page_number }}" class="page-link" aria-label="Previous">
                        <span aria-hidden="true">
                            <span class="lnr lnr-chevron-left"></span>
                        </span>
                    </a>
                </li>
                {% else %}
                <li class="page-item">
                    <a href="#" class="page-link" aria-label="Previous">
                        <span aria-hidden="true">
                            <span class="lnr lnr-chevron-left"></span>
                        </span>
                    </a>
                </li>
                {% endif %}

                {% for num in paginator.page_range %}
                    {% if num == currentPage %}
                        <li class="page-item active"><a href="/index/?page={{ num }}" class="page-link">{{ num }}</a></li>
                    {% else %}
                        <li class="page-item"><a href="/index/?page={{ num }}" class="page-link">{{ num }}</a></li>
                    {% endif %}
                {% endfor %}

                {% if post_list.has_next %}
                <li class="page-item">
                    <a href="/index/?page={{ post_list.next_page_number }}" class="page-link" aria-label="Next">
                        <span aria-hidden="true">
                            <span class="lnr lnr-chevron-right"></span>
                        </span>
                    </a>
                </li>
                {% else %}
                <li class="page-item">
                    <a href="#" class="page-link" aria-label="Next">
                        <span aria-hidden="true">
                            <span class="lnr lnr-chevron-right"></span>
                        </span>
                    </a>
                </li>
                {% endif %}
            </ul>
        </nav>
        
    </div>
</div>

{% endblock %}
  1. 如果页数十分多时,换另外一种显示方式
class IndexView(View):
    def get(self,request):
        post_list = Post.objects.all().order_by("-id")
        # 分页器
        paginator = Paginator(post_list,6)
        # print("count:",paginator.count)  # 数据总数
        # print("num_pages:",paginator.num_pages) # 总页数
        # print("page_range:",paginator.page_range) # 页码列表
        #
        # page1 = paginator.page(1)  # 第1页的page对象
        # for i in page1: # 遍历第1页的所有数据对象
        #     print(i)
        #
        # print("第一页的所有数据:",page1.object_list)  # 第1页的所有数据
        #
        # page2 = paginator.page(2)
        # print(page2.has_next())  # 是否有下一页
        # print(page2.next_page_number())  # 下一页的页码
        # print(page2.has_previous())  # 是否有上一页
        # print(page2.previous_page_number())  # 上一页的页码

        page = request.GET.get('page', 1)
        currentPage = int(page)

        #  如果页数十分多时,换另外一种显示方式
        if paginator.num_pages > 11:

            if currentPage - 5 < 1:
                pageRange = range(1, 11)
            elif currentPage + 5 > paginator.num_pages:
                pageRange = range(currentPage - 5, paginator.num_pages + 1)

            else:
                pageRange = range(currentPage - 5, currentPage + 6)

        else:
            pageRange = paginator.page_range

        try:
            print(page)
            post_list = paginator.page(page)
        except PageNotAnInteger:
            post_list = paginator.page(1)
        except EmptyPage:
            post_list = paginator.page(paginator.num_pages)

        return render(request,"index.html",locals())

Django ListView视图实现分页

  1. 在视图views.py文件中配置paginate_by
from django.views.generic.list import ListView
from .models import Post,Category
class IndexView(ListView):
    model = Post
    template_name = "index.html"
    context_object_name = "post_list"
    paginate_by = 6
    def get_queryset(self):
        post_list = Post.objects.all().order_by("-id")
        return post_list

    def get_context_data(self, *, object_list=None, **kwargs):
        kwargs['category_list'] = Category.objects.all().order_by('post_category') # 设置分类字段到模板上下文
        content = super(IndexView,self).get_context_data(**kwargs)
        print(content) # 检查是否有paginate对象
        return content
  1. 模板文件中编写分页语句
        <!-- 分页栏 -->
        <nav class="blog-pagination justify-content-center d-flex">
            <ul class="pagination">
                {% if page_obj.has_previous %}
                <li class="page-item">
                    <a href="{% url 'post:index' %}?page={{ page_obj.previous_page_number }}" class="page-link" aria-label="Previous">
                        <span aria-hidden="true">
                            <span class="lnr lnr-chevron-left"></span>
                        </span>
                    </a>
                </li>
                {% else %}
                <li class="page-item">
                    <a href="#" class="page-link" aria-label="Previous">
                        <span aria-hidden="true">
                            <span class="lnr lnr-chevron-left"></span>
                        </span>
                    </a>
                </li>
                {% endif %}

                {% for num in paginator.page_range %}
                    {% if num == page_obj.number %}
                        <li class="page-item active"><a href="{% url 'post:index' %}?page={{ num }}" class="page-link">{{ num }}</a></li>
                    {% else %}
                        <li class="page-item"><a href="{% url 'post:index' %}?page={{ num }}" class="page-link">{{ num }}</a></li>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                <li class="page-item">
                    <a href="{% url 'post:index' %}?page={{ page_obj.next_page_number }}" class="page-link" aria-label="Next">
                        <span aria-hidden="true">
                            <span class="lnr lnr-chevron-right"></span>
                        </span>
                    </a>
                </li>
                {% else %}
                <li class="page-item">
                    <a href="#" class="page-link" aria-label="Next">
                        <span aria-hidden="true">
                            <span class="lnr lnr-chevron-right"></span>
                        </span>
                    </a>
                </li>
                {% endif %}
            </ul>
        </nav>

    </div>
</div>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。