当前位置:   article > 正文

Flask使用paginate进行翻页_flask paginate

flask paginate

        Flask可以将数据库的表展示到网页上,如果数据过多,就需要借助flask中的paginate来对数据进行翻页操作,只需要两个操作就可实现数据的翻页

1.app.py

  1. #将MySQL的数据以表格的形式展示到网页
  2. @app.route('/admin/')
  3. def manager():
  4. page = int(request.args.get('page', 1))
  5. per_page = int(request.args.get('per_page', 10))
  6. paginate = Person.query.paginate(page=page, per_page=per_page, error_out=False)
  7. pagedata = paginate.items
  8. titles = ['序号', '用户名','电脑系统名', '年龄', '性别', '电话号码', '喜欢景点类型', '搜索景点类型', '搜索景点名称', '搜索景点评分', '账号创建时间']
  9. return render_template('manager.html',paginate=paginate, pagedata=pagedata,titles=titles)

Person为数据库的表的模式

 2.manager.html

  1. <!DOCTYPE html>
  2. {% extends 'base_main.html' %}
  3. {% block content %}
  4. <html lang="en">
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>旅游推荐系统!</title>
  8. <style>
  9. ul {
  10. list-style-type: none;
  11. margin: 0;
  12. padding: 0;
  13. overflow: hidden;
  14. }
  15. li {
  16. display: flex;
  17. justify-content: center;
  18. align-items: center;
  19. margin-top: 20px;
  20. margin-left:-5px;
  21. width: 70px;
  22. height: 50px;
  23. background-color: #deffff;
  24. color:white;
  25. }
  26. li::before {
  27. content: "";
  28. display: block;
  29. padding-top: 20%;
  30. background-color: #deffff;
  31. }
  32. body {
  33. background-image: url("https://img.zcool.cn/community/01d6d05c245575a8012029ac04c993.jpg@2o.jpg");
  34. filter: brightness(100%);
  35. background-repeat: no-repeat;
  36. background-size: cover;
  37. opacity: 0.7;
  38. }
  39. a.reset-style {
  40. background-color: initial;
  41. }
  42. input {
  43. width: 150px;
  44. height: 25px;
  45. }
  46. </style>
  47. </head>
  48. <body >
  49. <div >
  50. <div style="text-align:center"><h2>flask使用paginate翻页</h2></div>
  51. <table border="1" align="center" style="background-color: #ffffff;font-family:'楷体', sans-serif;text-align:center" >
  52. <thead>
  53. <tr>
  54. {% for i in titles %}
  55. <td>{{ i }}</td>
  56. {% endfor %}
  57. </tr>
  58. </thead>
  59. <tbody>
  60. {% for data in pagedata %}
  61. <tr>
  62. <td style="width:45px;">{{ loop.index0 }}</td>
  63. <td style="width:100px;">{{ data.username }}</td>
  64. <td style="width:100px;">{{ data.system_name }}</td>
  65. <td style="width:50px;">{{ data.age }}</td>
  66. <td style="width:50px;">{{ data.sex }}</td>
  67. <td style="width:100px;">{{ data.phone_number }}</td>
  68. <td style="width:100px;">{{ data.types }}</td>
  69. <td style="width:200px;">{{ data.search_type }}</td>
  70. <td style="width:100px;">{{ data.search_name }}</td>
  71. <td style="width:100px;">{{ data.search_score }}</td>
  72. <td style="width:100px;">{{ data.time }}</td>
  73. </tr>
  74. {% endfor %}
  75. </tbody>
  76. </table>
  77. <div>
  78. <ul style="text-align: center;">
  79. {% if paginate.has_prev %}
  80. <li style="display: inline-block;"><a href="/index/?page={{ paginate.prev_num }}">上一页</a></li>
  81. {% endif %}
  82. {% for i in paginate.iter_pages() %}
  83. {% if i == None %}
  84. <li style="display: inline-block;"><a >...</a></li>
  85. {% else %}
  86. <li style="display: inline-block;"><a href="/index/?page={{ i }}">{{ i }}</a></li>
  87. {% endif %}
  88. {% endfor %}
  89. {% if paginate.has_next %}
  90. <li style="display: inline-block;"><a href="/index/?page={{ paginate.next_num }}">下一页</a></li>
  91. {% endif %}
  92. </ul>
  93. </div>
  94. <div style="font-size: 12px; text-align: center; margin-top: 20px;margin-bottom: 10px;">
  95. 当前页数:{{ paginate.page }}
  96. 总页数:{{ paginate.pages }}
  97. 一共有{{ paginate.total }}条数据
  98. </div>
  99. </div>
  100. </body>
  101. {% endblock %}
  102. </html>

base_main.html的代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>My Website</title>
  5. <style>
  6. a {
  7. padding: 10px;
  8. color:black;
  9. text-decoration: none;
  10. marge-left:15px
  11. }
  12. a:hover {
  13. background-color: #FFFFFF; /* 更换背景色为红色 */
  14. }
  15. body {
  16. background-image: url("http://www.wlkst.com/u/cms/www/202006/17104548tne0.jpg");
  17. background-repeat: no-repeat;
  18. background-size: cover;
  19. opacity: 0.4;
  20. font-family:"微软雅黑", 'Arial', sans-serif;
  21. }
  22. body, html {
  23. margin: 0;
  24. padding: 0;
  25. width: 100%;
  26. height: 100%;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <nav style="background-color: #f1f1f1; padding: 10px;wigth:100%">
  32. <a href="/" style=" " >登录</a>
  33. <a href="/regist">注册</a>
  34. </nav>
  35. {% block content %}{% endblock %}
  36. </body>
  37. </html>

其中nav是给网页底部加导航栏,若不需要,可删除

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

闽ICP备14008679号