Flask使用paginate进行翻页

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

1.app.py

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

Person为数据库的表的模式

 2.manager.html

<!DOCTYPE html>
{% extends 'base_main.html' %}
{% block content %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>旅游推荐系统!</title>
    <style>
          ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
          }
          li {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
            margin-left:-5px;
            width: 70px;
            height: 50px;
            background-color: #deffff;
            color:white;
          }
          li::before {
            content: "";
            display: block;
            padding-top: 20%;
            background-color: #deffff;
          }
          body {
              background-image: url("https://img.zcool.cn/community/01d6d05c245575a8012029ac04c993.jpg@2o.jpg");
              filter: brightness(100%);
              background-repeat: no-repeat;
              background-size: cover;
              opacity: 0.7;
            }
          a.reset-style {
              background-color: initial;
            }
          input {
              width: 150px;
              height: 25px;
           }
        </style>
</head>
<body >
    <div >
        <div style="text-align:center"><h2>flask使用paginate翻页</h2></div>
        <table border="1" align="center" style="background-color: #ffffff;font-family:'楷体', sans-serif;text-align:center"  >
            <thead>
                <tr>
                    {% for i in titles %}
                        <td>{{ i }}</td>
                    {% endfor %}
                </tr>
            </thead>
            <tbody>
                {% for data in pagedata %}
                    <tr>
                        <td style="width:45px;">{{ loop.index0 }}</td>
                        <td style="width:100px;">{{ data.username }}</td>
                        <td style="width:100px;">{{ data.system_name }}</td>
                        <td style="width:50px;">{{ data.age }}</td>
                        <td style="width:50px;">{{ data.sex }}</td>
                        <td style="width:100px;">{{ data.phone_number }}</td>
                        <td style="width:100px;">{{ data.types }}</td>
                        <td style="width:200px;">{{ data.search_type }}</td>
                        <td style="width:100px;">{{ data.search_name }}</td>
                        <td style="width:100px;">{{ data.search_score }}</td>
                        <td style="width:100px;">{{ data.time }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
        <div>
            <ul style="text-align: center;">
                {% if paginate.has_prev %}
                    <li style="display: inline-block;"><a href="/index/?page={{ paginate.prev_num }}">上一页</a></li>
                {% endif %}
                {% for i in paginate.iter_pages() %}
                    {% if i == None %}
                        <li style="display: inline-block;"><a >...</a></li>
                    {% else %}
                        <li style="display: inline-block;"><a href="/index/?page={{ i }}">{{ i }}</a></li>
                    {% endif %}
                {% endfor %}
                {% if paginate.has_next %}
                    <li style="display: inline-block;"><a href="/index/?page={{ paginate.next_num }}">下一页</a></li>
                {% endif %}
            </ul>
        </div>
        <div style="font-size: 12px; text-align: center; margin-top: 20px;margin-bottom: 10px;">
            当前页数:{{ paginate.page }}
            总页数:{{ paginate.pages }}
            一共有{{ paginate.total }}条数据
        </div>
    </div>
</body>
{% endblock %}
</html>

base_main.html的代码如下:

<!DOCTYPE html>
<html>
<head>
  <title>My Website</title>
  <style>
        a {
            padding: 10px;
            color:black;
            text-decoration: none;
            marge-left:15px
        }
        a:hover {
            background-color: #FFFFFF; /* 更换背景色为红色 */
        }
        body {
              background-image: url("http://www.wlkst.com/u/cms/www/202006/17104548tne0.jpg");
              background-repeat: no-repeat;
              background-size: cover;
              opacity: 0.4;
              font-family:"微软雅黑", 'Arial', sans-serif;
            }
        body, html {
              margin: 0;
              padding: 0;
              width: 100%;
              height: 100%;
            }

    </style>
</head>
<body>
  <nav style="background-color: #f1f1f1; padding: 10px;wigth:100%">
    <a href="/" style=" " >登录</a>
    <a href="/regist">注册</a>
  </nav>
  {% block content %}{% endblock %}
</body>
</html>

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

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThinkPHP5是一款基于PHP的开源框架,它提供了许多方便的功能来简化开发过程。其中,paginate是ThinkPHP5框架自带的一个用于分页的方法。 paginate方法用于在查询结果中进行分页,以便在网页上展示数据。使用paginate方法,可以非常方便地实现数据的分页显示,并且框架会自动处理分页链接的生成和点击。 在使用paginate方法时,我们可以根据自己的需求进行自定义,以便满足特定的分页需求。具体的自定义方法如下: 首先,我们需要在模型文件中添加paginate方法的自定义配置。在模型文件中找到paginate方法,可以看到其实际上调用了系统的paginate方法。我们可以在模型文件中添加自定义配置,如: ```php public function paginate($listRows = 15, $simple = false, $config = []) { // 自定义分页配置 $config['query'] = request()->param(); $config['type'] = 'app\common\paginator\Bootstrap'; return parent::paginate($listRows, $simple, $config); } ``` 在上述代码中,我们可以看到对paginate方法进行了扩展,添加了$query和$type配置项。其中,$query用于保留当前页面的查询条件,而$type用于指定分页类的命名空间。我们可以根据自己的需求进行相应的配置。 接下来,在视图文件中生成分页链接时,我们可以通过自定义模板来实现样式的自定义。ThinkPHP5框架提供了内置的Bootstrap模板,在生成分页链接时可以指定使用该模板。具体的代码如下: ```php $paginate->render('app\common\paginator\Bootstrap'); ``` 在上述代码中,我们可以看到通过render方法指定了使用的分页模板。我们可以根据自己的需求指定其他模板,并进行样式的自定义。 综上所述,通过自定义paginate方法的配置和分页模板的选择,我们可以对ThinkPHP5框架中的paginate进行自定义,以便满足特定的分页需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值