1.views.py代码
wqjc_obj = models.FileUpload.objects.all()
page = Paginator(wqjc_obj, 10) # 把wqjc_obj进行分页,每页显示十页数据
page_id = request.GET.get('page_id') # 获取前台点击时传来的分页数据
if page_id:
try:
wqjcs = page.page(page_id) # 取到当前页码数据
except PageNotAnInteger: # 传递非整数ID页码,返回到第一页
wqjcs = page.page(1)
except EmptyPage:
wqjcs = page.page(1) # 传递越界ID,传递ID数超过总页数,返回第一页
else:
wqjcs = page.page(1)
return render(request, 'html/wqjc.html', locals())
2.html代码
<div id="table">
<div >
{% for wqjc in wqjcs %}
<a href="/south/detail/{{wqjc.id}}" class="n">{{ wqjc.name }}</a>
<span class="date">{{ wqjc.add_time }}</span><br>
{% endfor %}
</div>
</div>
<!--分页-->
<div class="fy">
<!--如果有上一页,显示上一页按钮 -->
{% if wqjcs.has_previous %}
<!--previous_page_number, 上一页 -->
<a href="/south/wqjc?page_id={{wqjcs.previous_page_number}}">
<button><<</button>
</a>
{% endif %}
<!--遍历总页数page.page_range -->
{% for i in page.page_range %}
<a href="/south/wqjc?page_id={{i}}">
<button>{{i}}</button>
</a>
{% endfor %}
<!--判断是否有下一页 -->
{% if wqjcs.has_next %}
<!--next_page_number,下一页-->
<a href="/south/wqjc?page_id={{wqjcs.next_page_number}}">
<button> >> </button>
</a>
{% endif %}
</div>