Django簡(jiǎn)單博客實(shí)戰(zhàn)(四)--- 分頁(yè)實(shí)現(xiàn)

Django實(shí)現(xiàn)分頁(yè)

項(xiàng)目地址:https://github.com/ylpxzx/lifeblog

Django自帶分頁(yè)組件實(shí)現(xiàn)分頁(yè)器

  1. 視圖函數(shù)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")
        
        # 分頁(yè)器
        paginator = Paginator(post_list,6)
        
        # print("count:",paginator.count)  # 數(shù)據(jù)總數(shù)
        # print("num_pages:",paginator.num_pages) # 總頁(yè)數(shù)
        # print("page_range:",paginator.page_range) # 頁(yè)碼列表
        #
        # page1 = paginator.page(1)  # 第1頁(yè)的page對(duì)象
        # for i in page1: # 遍歷第1頁(yè)的所有數(shù)據(jù)對(duì)象
        #     print(i)
        #
        # print("第一頁(yè)的所有數(shù)據(jù):",page1.object_list)  # 第1頁(yè)的所有數(shù)據(jù)
        #
        # page2 = paginator.page(2)
        # print(page2.has_next())  # 是否有下一頁(yè)
        # print(page2.next_page_number())  # 下一頁(yè)的頁(yè)碼
        # print(page2.has_previous())  # 是否有上一頁(yè)
        # print(page2.previous_page_number())  # 上一頁(yè)的頁(yè)碼

        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. 模板文件中編寫分頁(yè)語(yǔ)句
{% block area %}
<div class="col-lg-8">
    <div class="blog_left_sidebar">
    {% for post in post_list %}
        <li>{{ post }}</li>
    {% endfor %}
        
        <!-- 分頁(yè)欄 -->
        <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. 如果頁(yè)數(shù)十分多時(shí),換另外一種顯示方式
class IndexView(View):
    def get(self,request):
        post_list = Post.objects.all().order_by("-id")
        # 分頁(yè)器
        paginator = Paginator(post_list,6)
        # print("count:",paginator.count)  # 數(shù)據(jù)總數(shù)
        # print("num_pages:",paginator.num_pages) # 總頁(yè)數(shù)
        # print("page_range:",paginator.page_range) # 頁(yè)碼列表
        #
        # page1 = paginator.page(1)  # 第1頁(yè)的page對(duì)象
        # for i in page1: # 遍歷第1頁(yè)的所有數(shù)據(jù)對(duì)象
        #     print(i)
        #
        # print("第一頁(yè)的所有數(shù)據(jù):",page1.object_list)  # 第1頁(yè)的所有數(shù)據(jù)
        #
        # page2 = paginator.page(2)
        # print(page2.has_next())  # 是否有下一頁(yè)
        # print(page2.next_page_number())  # 下一頁(yè)的頁(yè)碼
        # print(page2.has_previous())  # 是否有上一頁(yè)
        # print(page2.previous_page_number())  # 上一頁(yè)的頁(yè)碼

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

        #  如果頁(yè)數(shù)十分多時(shí),換另外一種顯示方式
        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視圖實(shí)現(xiàn)分頁(yè)

  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') # 設(shè)置分類字段到模板上下文
        content = super(IndexView,self).get_context_data(**kwargs)
        print(content) # 檢查是否有paginate對(duì)象
        return content
  1. 模板文件中編寫分頁(yè)語(yǔ)句
        <!-- 分頁(yè)欄 -->
        <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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容