flask sqlalchemy pagination

flask 利用 flask_sqlalchemy分页

page=request.args.get('page', 1, type=int)
  pagination = Device.query.paginate(page)
  list_view = pagination.items

  return render_template('backend/devices/index.html', list_view = list_view, pagination = pagination)
{% for data in list_view %}
          <tr>
            <td>{{ data.name }}</td>
            <td class="tree_item tree_item_{{ data.parent_id }}">{{ data.device_sn }}</td>
            <td>
              <a href="{{ url_for('device_views.edit', id = data.id) }}">修改</a>
              <a href="javascript:void(0)" class="p_delete_action" data-method="POST" data-url="{{ url_for('device_views.delete', id = data.id) }}">删除</a>
            </td>
          </tr>
          {% endfor %}
<ul class="pagination justify-content-end">
            <i>共 {{ pagination.pages }} 页 {{ pagination.total }} 条记录</i>
            <li class="page-item"><a class="page-link" href="{{ url_for('device_views.index') }}?page=1">首 页</a></li>
            {% if pagination.has_prev %}
            <li class="page-item"><a class="page-link" href="{{ url_for('device_views.index') }}?page={{ pagination.prev_num }}">上一页</a></li>
            {% endif %}

            {% for i in pagination.iter_pages() %}
            <li class="page-item"><a class="page-link" href="{{ url_for('device_views.index') }}?page={{ i }}">{{ i }}</a></li>
            {% endfor %}

            {% if pagination.has_next %}
            <li class="page-item"><a class="page-link" href="{{ url_for('device_views.index') }}?page={{ pagination.next_num }}">下一页</a></li>
            {% endif %}
            <li class="page-item"><a class="page-link" href="{{ url_for('device_views.index') }}?page={{ pagination.pages }}">尾 页</a></li>
          </ul>

 

https://flask-sqlalchemy.palletsprojects.com/en/2.x/api/#flask_sqlalchemy.Pagination

 

has_next

True if a next page exists.

has_prev

True if a previous page exists

items = None

the items for the current page

iter_pages(left_edge=2left_current=2right_current=5right_edge=2)

Iterates over the page numbers in the pagination. The four parameters control the thresholds how many numbers should be produced from the sides. Skipped page numbers are represented as None. This is how you could render such a pagination in the templates:

{% macro render_pagination(pagination, endpoint) %}
  <div class=pagination>
  {%- for page in pagination.iter_pages() %}
    {% if page %}
      {% if page != pagination.page %}
        <a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
      {% else %}
        <strong>{{ page }}</strong>
      {% endif %}
    {% else %}
      <span class=ellipsis>…</span>
    {% endif %}
  {%- endfor %}
  </div>
{% endmacro %}

next(error_out=False)

Returns a Pagination object for the next page.

next_num

Number of the next page

page = None

the current page number (1 indexed)

pages

The total number of pages

per_page = None

the number of items to be displayed on a page.

prev(error_out=False)

Returns a Pagination object for the previous page.

prev_num

Number of the previous page.

query = None

the unlimited query object that was used to create this pagination object.

total = None

the total number of items matching the query

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值