【django不分离项目】使用内置分页
from django.core.paginator import Paginator #自带分页
from Databases import models
# 数据首页
def index(request):
article = models.Article.objects.all()
paginator = Paginator(article, 5) # 按每页5条分页
page = request.GET.get('page', '1') # 默认跳转到第一页
result = paginator.page(page)
return render(request, "index.html",{"article":result})
路由:
# 首页
url(r'^$', index.index),
{% for article_obj in article %}
{% comment %} 文章渲染 {% endcomment %}
<div class="single-post">
<div class="inner-post">
<div class="post-img">
<a href="#"><img src="/media/{{ article_obj.image }}" alt="blog"></a>
</div>
<div class="post-info">
<div class="post-title">
<h3><a href="#">{{ article_obj.title }}</a></h3>
</div>
<div class="post-content">
<p>{{ article_obj.desc }}</p>
</div>
<div class="blog-meta fix">
<div class="meta-left pull-left">
<ul>
<li><span class="flaticon-man-user user"></span>
<p>By <a href="#">Admin</a></p></li>
<li><span class="flaticon-calendar clendar"></span>
<p>{{ article_obj.time }}</p></li>
</ul>
</div>
<div class="post-readmore pull-right">
<a href="#" class="readmore-btn">Read More <span>+</span></a>
</div>
</div>
</div>
</div>
<div class="post-date one">
<span>{{ article_obj.id }}</span>
</div>
</div>
{% endfor %}
{% comment %} 文章渲染 {% endcomment %}
<div class="pagination">
{% comment %} 分页渲染 {% endcomment %}
<ul>
<li class="prev"><a href="/?page=1">首页</a></li>
{% if article.has_previous %}
<li class="page active"><a href="/?page={{ article.previous_page_number }}">上一页</a></li>
{% endif %}
{% for num in article.paginator.page_range %}
<li class="page"><a href="/?page={{ num }}">{{ num }}</a></li>
{% endfor %}
{% if messages.has_next %}
<li class="next pull-right"><a href="/?page={{ article.next_page_number }}">下一页</a></li>
{% endif %}
<li class="next pull-right"><a href="/?page={{ article.paginator.num_pages }}">尾页</a></li>
</ul>
{% comment %} 分页渲染 {% endcomment %}
</div>
Python全栈(后端、数据分析、脚本、爬虫、EXE客户端) / 前端(WEB,移动,H5) / Linux / SpringBoot / 机器学习