当前位置:   article > 正文

Python--django实现分页功能_python django 分页

python django 分页

1.html测试数据

  1. {% extends 'crm_/main_table.html' %}
  2. {% block css %}
  3. <style>
  4. th {
  5. text-align: center;
  6. }
  7. </style>
  8. {% endblock %}
  9. {% block table_ %}
  10. <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
  11. <div class="table-responsive text-center">
  12. <table class="table table-striped table-hover table-bordered main">
  13. <thead>
  14. <tr>
  15. <th>#</th>
  16. <th>称号</th>
  17. </tr>
  18. </thead>
  19. <tbody>
  20. {% for l in ls %}
  21. <tr>
  22. <td>{{ forloop.counter }}</td>
  23. <td>{{ l.name }}</td>
  24. <td>{{ l.pwd }}</td>
  25. </tr>
  26. {% endfor %}
  27. </tbody>
  28. </table>
  29. </div>
  30. <nav aria-label="Page navigation">
  31. <ul class="pagination">
  32. {{ st_pr }}
  33. {{ pads }}
  34. {{ st_en }}
  35. </ul>
  36. </nav>
  37. </div>
  38. {% endblock %}

2.封装的分页功能

  1. '''
  2. 分页器
  3. '''
  4. from django.utils.safestring import mark_safe
  5. class Pagination:
  6. def __init__(self, request, all_data, pag_data=10, max_pad=11):
  7. # 基本的URL
  8. self.base_url = request.path_info
  9. # 第一次返回时没有id或则是你输入的id不存在
  10. try:
  11. self.current_pad = int(request.GET.get('id'))
  12. except Exception as e:
  13. self.current_pad = 1
  14. ls = [{'name': 'alex{0}'.format(i), 'pwd': 'dsb{0}'.format(i)} for i in range(1, 302)]
  15. # 最多显示的分页个数
  16. self.max_pad = max_pad
  17. half_pad = self.max_pad // 2
  18. # 定义你想要显示的数据
  19. self.pag_data = pag_data
  20. # 拿到总数据数
  21. self.all_data = all_data
  22. # 用divmod内置函数,取到你要分的页数
  23. self.pads, pad_y = divmod(self.all_data, self.pag_data)
  24. if pad_y:
  25. self.pads += 1
  26. # 定义分页的开始位置和结束位置
  27. if self.current_pad <= half_pad:
  28. self.start_pad = 1
  29. self.end_pad = self.max_pad
  30. elif self.current_pad >= self.pads - half_pad:
  31. self.start_pad = self.pads - self.max_pad + 1
  32. self.end_pad = self.pads + 1
  33. else:
  34. self.start_pad = self.current_pad - half_pad
  35. self.end_pad = self.current_pad + half_pad
  36. '''
  37. 1 1 10
  38. 2 10 20
  39. 3 20 30
  40. '''
  41. # 如果你的总页数小于你定义最大显示,改变它的开始和结束的位置
  42. if self.pads <= self.max_pad:
  43. self.start_pad = 1
  44. self.end_pad = self.pads + 1
  45. @property
  46. def StartPage(self):
  47. return (self.current_pad - 1) * 10
  48. @property
  49. def EndPage(self):
  50. return self.current_pad * 10
  51. def ActivePage(self):
  52. # 把前端代码拿过来,最后用""字符串拼接,然后再用mark_safe给标签过滤
  53. lst = []
  54. for i in range(self.start_pad, self.end_pad):
  55. if i == self.current_pad:
  56. st = '<li class="active"><a href="/pad/?id={0}">{0}</a></li>'.format(i)
  57. else:
  58. st = '<li><a href="/pad/?id={0}">{0}</a></li>'.format(i)
  59. lst.append(st)
  60. str_ls = mark_safe("".join(lst))
  61. return str_ls
  62. def PrePage(self):
  63. # 上一页
  64. if self.current_pad <= 1:
  65. st_pr = '<li class="disabled"><span aria-hidden="true">&laquo;</span></li>'
  66. else:
  67. st_pr = '<li><a href="/pad/?id={}" aria-label="Previous">' \
  68. '<span aria-hidden="true">&laquo;</span></a></li>'.format(
  69. self.current_pad - 1)
  70. st_pr = mark_safe(st_pr)
  71. return st_pr
  72. def NextPage(self):
  73. # 下一页
  74. if self.current_pad >= self.pads:
  75. st_en = '<li class="disabled"><span aria-hidden="true">&raquo;</span></li>'
  76. else:
  77. st_en = '<li><a href="/pad/?id={}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'.format(
  78. self.current_pad + 1)
  79. st_en = mark_safe(st_en)
  80. return st_en

3.view文件中调用:

  1. user = [{'name': 'alex{}'.format(i), 'pwd': 'alexdsb{}'.format(i)} for i in range(1, 302)]
  2. def pad(request):
  3. obj = Pagination(request, len(user))
  4. return render(request, 'paddingex.html', {
  5. 'ls': user[obj.StartPage:obj.EndPage],
  6. 'pads': obj.ActivePage(),
  7. 'st_pr': obj.PrePage(),
  8. 'st_en': obj.NextPage()
  9. })

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/203715
推荐阅读
相关标签
  

闽ICP备14008679号