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=2, left_current=2, right_current=5, right_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