3

flask如何实现内容分页功能

 2 years ago
source link: https://www.huhexian.com/45694.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

flask如何实现内容分页功能

2022-05-2612:04:15评论

在web开发中,分页是必不可少的功能,Flask实现展示内容的分页也非常简单,这里通过实例来学习一下Flask如何为网站分页。

首先,自定义一个分页工具类page_utils:

  1. from urllib import urlencode
  2. class Pagination(object):
  3. def __init__(self, current_page, total_count, base_url, params, per_page_count=10, max_pager_count=11):
  4. current_page = int(current_page)
  5. except Exception as e:
  6. current_page = 1
  7. if current_page <=0:
  8. current_page = 1
  9. self.current_page = current_page
  10. self.total_count = total_count
  11. self.per_page_count = per_page_count
  12. max_page_num, div = divmod(total_count, per_page_count)
  13. if div:
  14. max_page_num += 1
  15. self.max_page_num = max_page_num
  16. self.max_pager_count = max_pager_count
  17. self.half_max_pager_count = int((max_pager_count - 1) / 2)
  18. self.base_url = base_url
  19. import copy
  20. params = copy.deepcopy(params)
  21. get_dict = params.to_dict()
  22. self.params = get_dict
  23. @property
  24. def start(self):
  25. return (self.current_page - 1) * self.per_page_count
  26. @property
  27. def end(self):
  28. return self.current_page * self.per_page_count
  29. def page_html(self):
  30. if self.max_page_num <= self.max_pager_count:
  31. pager_start = 1
  32. pager_end = self.max_page_num
  33. # 如果总页数 > 11
  34. else:
  35. if self.current_page <= self.half_max_pager_count:
  36. pager_start = 1
  37. pager_end = self.max_pager_count
  38. else:
  39. if (self.current_page + self.half_max_pager_count) > self.max_page_num:
  40. pager_end = self.max_page_num
  41. pager_start = self.max_page_num - self.max_pager_count + 1 #倒这数11个
  42. else:
  43. pager_start = self.current_page - self.half_max_pager_count
  44. pager_end = self.current_page + self.half_max_pager_count
  45. page_html_list = []
  46. self.params['page'] = 1
  47. first_page = '<li><a href="%s?%s">首页</a></li>'.decode("utf-8") % (self.base_url,urlencode(self.params),)
  48. page_html_list.append(first_page)
  49. self.params["page"] = self.current_page - 1
  50. if self.params["page"] < 1:
  51. pervious_page = '<li class="disabled"><a href="%s?%s" aria-label="Previous">上一页</span></a></li>'.
  52. decode("utf-8") % (self.base_url, urlencode(self.params))
  53. else:
  54. pervious_page = '<li><a href = "%s?%s" aria-label = "Previous" >上一页</span></a></li>'.decode("utf-8") %
  55. ( self.base_url, urlencode(self.params))
  56. page_html_list.append(pervious_page)
  57. # 中间页码
  58. for i in range(pager_start, pager_end + 1):
  59. self.params['page'] = i
  60. if i == self.current_page:
  61. temp = '<li class="active"><a href="%s?%s">%s</a></li>' % (self.base_url,urlencode(self.params), i,)
  62. else:
  63. temp = '<li><a href="%s?%s">%s</a></li>' % (self.base_url,urlencode(self.params), i,)
  64. page_html_list.append(temp)
  65. self.params["page"] = self.current_page + 1
  66. if self.params["page"] > self.max_page_num:
  67. self.params["page"] = self.current_page
  68. next_page = '<li class="disabled"><a href = "%s?%s" aria-label = "Next">下一页</span></a></li >'.decode
  69. ("utf-8") % (self.base_url, urlencode(self.params))
  70. else:
  71. next_page = '<li><a href = "%s?%s" aria-label = "Next">下一页</span></a></li>'.decode("utf-8") %
  72. (self.base_url, urlencode(self.params))
  73. page_html_list.append(next_page)
  74. self.params['page'] = self.max_page_num
  75. last_page = '<li><a href="%s?%s">尾页</a></li>'.decode("utf-8") % (self.base_url, urlencode(self.params),)
  76. page_html_list.append(last_page)
  77. return ''.join(page_html_list)

自定义方法中的参数:

current_page——表示当前页。

total_count——表示数据总条数。

base_url——表示分页URL前缀,请求的前缀获取可以通过Flask的request.path方法,无需自己指定。

例如:我们的路由方法为@app.route('/test'),request.path方法即可获取/test。

params——表示请求传入的数据,params可以通过request.args动态获取。

例如:我们链接点击为:http://localhost:5000/test?page=10,此时request.args获取数据为ImmutableMultiDict([('page', u'10')])

per_page_count——指定每页显示数。

max_pager_count——指定页面最大显示页码

接着,我们使用一个测试方法来使用这个工具类,达到分页效果,test.py:

  1. from <a href="http://www.gaodaima.com/tag/flask" title="查看更多关于flask的文章" target="_blank">flask</a> import Flask, render_template, request
  2. from page_utils import Pagination
  3. app = Flask(__name__)
  4. @app.route('/test')
  5. def test():
  6. li = []
  7. for i in range(1, 100):
  8. li.append(i)
  9. pager_obj = Pagination(request.args.get("page", 1), len(li), request.path, request.args, per_page_count=10)
  10. print(request.path)
  11. print(request.args)
  12. index_list = li[pager_obj.start:pager_obj.end]
  13. html = pager_obj.page_html()
  14. return render_template("obj/test.html", index_list=index_list, html=html)
  15. if __name__ == '__main__':
  16. app.run(debug=True)

在上面的程序中,li为我们要分页的对象,数组list,我们获取到这个list之后,把他用工具类中的起止方法包起来。

传递数据用包装后的list,这样就达到了需要哪一段数据我们传递哪一段的效果,包装的方法:index_list = li[pager_obj.start:pager_obj.end]

我们用一个HTML页面去显示它,分页样式不是重点,我们这里直接引入bootstrap封装好的分页效果,代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <link rel="stylesheet" href="{{%20url_for('static',%20filename='css/bootstrap.min.css')%20}}">
  7. <style>
  8. .container{
  9. margin-top: 20px;
  10. </style>
  11. </head>
  12. <body>
  13. <div>
  14. <div class="row " style="margin-top: 10px">
  15. <ul>
  16. {% for foo in index_list %}
  17. <li>{{ foo }}:这是列表内容~~</li>
  18. {% endfor %}
  19. </ul>
  20. <nav aria-label="Page navigation">
  21. <ul>
  22. {{ html|safe }}
  23. </ul>
  24. </nav>
  25. </div>
  26. </div>
  27. </body>
  28. </html>

这样一个分页的效果就做好了!

flask如何实现内容分页功能

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK