{oldboy-django][2深入django]分页功能
Posted liuzhipenglove
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了{oldboy-django][2深入django]分页功能相关的知识,希望对你有一定的参考价值。
1 django自带分页
1.1 分页模板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css"> </head> <body> <p>用户信息</p> <table class="table"> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>type</th> </tr> </thead> <tbody> {% for row in current_page_obj %} <tr> <td>{{ row.id }}</td> <td>{{ row.name }}</td> <td>{{ row.age }}</td> <td>{{ row.ut.title }}</td> </tr> {% endfor %} </tbody> </table> <div> {% if current_page_obj.has_previous %} <a href="/fenye/?page={{ current_page_obj.previous_page_number }}">上一页</a> {% endif %} {% for page in current_page_obj.paginator.page_range %} <a href="/fenye/?page={{ page }}">{{ page }}</a> {% endfor %} {% if current_page_obj.has_next %} <a href="/fenye/?page={{ current_page_obj.next_page_number }}">下一页</a> {% endif %} </div> </body> </html>
1.2 分页视图函数
def fenye(request): user_list = models.UserInfo.objects.all() # 实现分页功能 from django.core.paginator import Page,Paginator current_page_number = request.GET.get(‘page‘) paginator = Paginator(user_list, 10) # print(paginator.count) #数据总行数 305 # print(paginator.num_pages) #总页数 31 # print(paginator.object_list) #当前页的数据 QuerySet[obj,obj] # print(paginator.per_page) #每一页显示多少行数据10 # print(paginator.page_range) #页数的范围(1,32), 是一个range(1,32),即是一个数组 # print(paginator.page(1)) #里面的1表示页码数, try: current_page_obj = paginator.page(current_page_number) # 也是一个QuerySet对象 [obj,obj,..] except Exception as e: current_page_obj = paginator.page(1) # print(current_page_obj.has_next()) #当前页是否有下一页 # print(current_page_obj.next_page_number()) #当前页的下一页码 # print(current_page_obj.has_previous()) # 当前页是否有上一页 # print(current_page_obj.previous_page_number())# 当前页的上一页码 # print(current_page_obj.object_list) # 当前页的数据行列表QuerySet[obj,obj,...] # print(current_page_obj.number) # 当前页码 # print(current_page_obj.paginator) # 当前页的paginator对象 return render(request, ‘fenye.html‘, {‘current_page_obj‘: current_page_obj})
2 自定义分页
2.1 模板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css"> </head> <body> <p>用户信息</p> <table class="table"> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>type</th> </tr> </thead> <tbody> {% for row in user_list %} <tr> <td>{{ row.id }}</td> <td>{{ row.name }}</td> <td>{{ row.age }}</td> <td>{{ row.ut.title }}</td> </tr> {% endfor %} </tbody> </table> {# {{ page_info.pager|safe }}#} <nav aria-label="Page navigation"> <ul class="pagination"> {{ page_info.pager|safe }} </ul> </nav> {# <div>#} {##} {# {% if current_page_obj.has_previous %}#} {# <a href="/fenye/?page={{ current_page_obj.previous_page_number }}">上一页</a>#} {# {% endif %}#} {# {% for page in current_page_obj.paginator.page_range %}#} {# <a href="/fenye/?page={{ page }}">{{ page }}</a>#} {# {% endfor %}#} {##} {# {% if current_page_obj.has_next %}#} {# <a href="/fenye/?page={{ current_page_obj.next_page_number }}">下一页</a>#} {# {% endif %}#} {##} {##} {##} {# </div>#} </body> </html>
2.2 自定义模块
#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day4.settings") try: from django.core.management import execute_from_command_line except ImportError: # The above import may fail for some other reason. Ensure that the # issue is really that Django is missing to avoid masking other # exceptions on Python 2. try: import django except ImportError: raise ImportError( "Couldn‘t import Django. Are you sure it‘s installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) raise execute_from_command_line(sys.argv)
2.3 视图
def custom_fenye(request): from utils.pagation_define import PageInfo current_page_number = request.GET.get(‘page‘) all_count = models.UserInfo.objects.all().count() page_info = PageInfo(current_page_number, all_count, "/custom_fenye.html/") user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()] # 列表切片操作 a[start:end] start<= index < end return render(request, ‘custom_fenye.html‘, {‘user_list‘: user_list, ‘page_info‘: page_info})
以上是关于{oldboy-django][2深入django]分页功能的主要内容,如果未能解决你的问题,请参考以下文章
[oldboy-django][2深入django]老师管理--查看,添加,编辑
[oldboy-django][2深入django]初始Form组件
[oldboy-django][2深入django]点击刷新验证码
[oldboy-django][2深入django]学生管理(Form)--查看(分页)