我使用 Flask 和 Peewee 创建了一个练习博客。我想在应用程序中包含分页。我遵循了 Colifier 示例博客分页,它工作正常。我的问题,有没有办法让我显示这样的页数:
这是我遵循的示例分页:
这是分页代码的样子:
分页.html
{% if pagination.get_page_count() > 1 %}
<ul class="pager">
{% if pagination.get_page() > 1 %}
<li class="previous"><a href="./?{{ request.args|clean_querystring('page', page=pagination.get_page() - 1) }}">« Previous {{ pagination.get_page() - 1 }} / {{ pagination.get_page_count() }}</a></li>
{% else %}
<li class="previous disabled"><a href="#">« Previous</a></li>
{% endif %}
{% if pagination.get_page_count() > pagination.get_page() %}
<li class="next"><a href="./?{{ request.args|clean_querystring('page', page=pagination.get_page() + 1) }}">Next {{ pagination.get_page() + 1 }} / {{ pagination.get_page_count() }} »</a></li>
{% else %}
<li class="next disabled"><a href="#">Next »</a></li>
{% endif %}
</ul>
{% endif %}
初始化.py
@app.template_filter('clean_querystring')
def clean_querystring(request_args, *keys_to_remove, **new_values):
querystring = dict((key, value) for key, value in request_args.items())
for key in keys_to_remove:
querystring.pop(key, None)
querystring.update(new_values)
return urllib.parse.urlencode(querystring)
编辑:我在分页模板中添加了这个,但我认为这看起来不对
# Pagination numbers
{% for page_num in page %}
{% if page_num %}
{% if page.pages == page_num %}
<a href="{{ request.args|clean_querystring('page', page=pagination.get_page()) }}" class="btn btn-primary">{{page_num}}</a>
{% else %}
<a href="{{ request.args|clean_querystring('page', page=pagination.get_page()) }}" class="btn btn-outline-primary">{{page_num}}</a>
{% endif %}
{% endif %}
{% endfor %}