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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狼性书生

谢谢鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值