python jinja2模板分页效果

html

    <div class="vendor-paging"><!--paging -->
      <div class='vendor-page-numbers'><!-- page button -->
        {% if vendor_page != 1 %}<a class='vendor-page-left-right page-pre' href='javascript:void(0);'><img class='left-right-image' src='/static/images/scroll_left.png'/></a>{% endif %}
        {% for i in range(1, page_total + 1) %}
          <a class='page-digital {% if vendor_page == i %}page-selected{% endif %}' href='javascript:void(0);' num="{{ i }}"><span class='page-number'>{{i}}</span></a>
        {% endfor %}
        {% if vendor_page != page_total and page_total%}<a class='vendor-page-left-right page-next' href='javascript:void(0);'><img class='left-right-image' src='/static/images/scroll_right.png'/></a>{% endif %}
      </div><!-- page button -->
      <div class="show-page-info font-s-14">
        <div class="gl-inline" style="line-height: 30px;">共{{ total }}家 ({% if total %}{{ start}}{% else %}0{% endif %}-{% if vendor_page < page_total%}{{ vendor_page * count }}{% else %}{{ total }}{% endif %}) 显示</div>
        <div class="select-style gl-inline" style="width: 135px;">
        <select id="page-number" class="select">
          <option value="1" {% if count == 1 %}selected{% endif %}>1每页</option>
          <option value="20" {% if count == 20 %}selected{% endif %}>20每页</option>
          <option value="30" {% if count == 30 %}selected{% endif %}>30每页</option>
          <option value="50" {% if count == 50 %}selected{% endif %}>50每页</option>
          <option value="100" {% if count == 100 %}selected{% endif %}>100每页</option>
          <option value="250" {% if count == 250 %}selected{% endif %}>250每页</option>
        </select>
        </div>
      </div>
    </div><!--paging -->

<input name="current_page" id="current_page" value={{ current_page }} type="hidden"></input>
<input name="count" id="count" value={{ count }} type="hidden"></input>

python

#pagination
current_page = int(params['current_page'])
count = int(params['count'])
start = (current_page - 1) * count
params['start'] = start
second_level_url = params['second_level_url']
params['op'] = "getVendorProducts"
params['count'] = count
vendor_cars, car_total = rpc('vendor', params)
page_total = int(ceil(float(car_total) / count))

转载于:https://www.cnblogs.com/xuedong09/p/3173725.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用 Flask 和 pymysql 实现分页的示例代码: ```python from flask import Flask, render_template, request import pymysql app = Flask(__name__) @app.route('/') def index(): # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='mydb', charset='utf8mb4') cur = conn.cursor() # 获取分页参数 page_num = int(request.args.get('page', 1)) # 当前页码,默认为 1 page_size = 10 # 每页显示 10 条记录 # 计算 LIMIT 和 OFFSET 的值 limit = page_size offset = (page_num - 1) * page_size # 构造 SQL 语句 sql = "SELECT * FROM mytable LIMIT %s OFFSET %s" # 执行查询 cur.execute(sql, [limit, offset]) rows = cur.fetchall() # 获取总记录数 cur.execute("SELECT COUNT(*) FROM mytable") total_count = cur.fetchone()[0] # 计算总页数 total_page = (total_count + page_size - 1) // page_size # 关闭数据库连接 cur.close() conn.close() # 渲染模板 return render_template('index.html', rows=rows, page_num=page_num, total_page=total_page) if __name__ == '__main__': app.run() ``` 在示例代码中,我们使用 Flask 框架实现了一个简单的分页功能。具体步骤如下: 1. 定义路由 `/`,用于处理分页请求。 2. 在路由函数中,获取分页参数 `page_num` 和 `page_size`。 3. 根据分页参数计算 LIMIT 和 OFFSET 的值。 4. 构造 SQL 语句,执行查询,获取查询结果 `rows`。 5. 获取总记录数,计算总页数 `total_page`。 6. 关闭数据库连接。 7. 渲染模板,将查询结果 `rows`、当前页码 `page_num` 和总页数 `total_page` 传递给模板。 在模板中,我们可以使用 Jinja2 模板引擎来渲染分页导航条和查询结果。分页导航条可以使用 Bootstrap 的分页组件来实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值