view.py
# 分页 def paging(request): # 丛数据库表取出来的数据总数 count_data = len(message.objects.all()) # 当前页数,因为url的传值是字符串,所以需要转成整形 page = int(request.GET.get("page")) # 每页显示3条数据 per_page_count = 3 # 开始的索引 start_dat = (page - 1) * per_page_count # 结束的索引 end_data = page * per_page_count # 数据切片 data = message.objects.all()[start_dat:end_data] # 上一页 prev_pager = page - 1 # 下一页 next_pager = page + 1 return render(request,"paging.html",{"list":data,"prev_pager":prev_pager,"next_pager":next_pager})
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>分页</title> </head> <body> <h1>信息展示</h1> <table> <tr> <th>用户名</th> <th>密码</th> </tr> {% for line in list %} <tr> <td>{{line.username}}</td> <td>{{line.password}}</td> </tr> {% endfor %} </table> <a href="{% url "paging" %}?page={{ prev_pager }}">上一页</a> <a href="{% url "paging" %}?page={{ next_pager }}">下一页</a> </body> </html>
urls.py
url(r'^paging.html$', views.paging,name='paging'),