In the process of learning, we will encounter such a problem, that is, in the process of learning, we will find that paging is needed. Here, I will introduce to you the paging mentioned in the book.
@app.route('/',methods=['GET']) @app.route('/<int:page>') def home(page=1): pagination=Post.query.order_by(Post.publish_date.desc()).paginate(page, per_page=10,error_out=False) posts = pagination.items link,tuijian_post,fenlei=get_tui_link() return render_template('home1.html', posts=posts, pagination=pagination, tuijian_post=tuijian_post,fenleis=fenlei, links=link)
This is the paginated data I read from the database. So how do we paginate? Let’s see what the book says
Then we need to use a separate page to save our paging related information.
{% macro pagination_widget(pagination, endpoint) %} <ul class="pagination"> <li{% if not pagination.has_prev %} class="disabled"{% endif %}> <a style='background-color: lightgoldenrodyellow;color: brown;' href="{% if pagination.has_prev %}{{ url_for(endpoint, page = pagination.page - 1, **kwargs) }}{% else %}#{% endif %}"> « </a> </li> {% for p in pagination.iter_pages() %} {% if p %} {% if p == pagination.page %} <li class="active"> <a style='background-color: lightgoldenrodyellow;color: brown;' href="{{ url_for(endpoint, page = p, **kwargs) }}">{{ p }}</a> </li> {% else %} <li> <a style='background-color: lightgoldenrodyellow;color: brown;' href="{{ url_for(endpoint, page = p, **kwargs) }}">{{ p }}</a> </li> {% endif %} {% else %} <li class="disabled"><a href="#">…</a></li> {% endif %} {% endfor %} <li{% if not pagination.has_next %} class="disabled"{% endif %}> <a style='background-color: lightgoldenrodyellow;color: brown;' href="{% if pagination.has_next %}{{ url_for(endpoint, page = pagination.page + 1, **kwargs) }}{% else %}#{% endif %}"> » </a> </li> </ul> {% endmacro %}
So how do we use it
{% import "mac.html" as macros %}
Add the following
## after our loop
# The effect is as shownThe above is the detailed content of Detailed introduction to flask paging. For more information, please follow other related articles on the PHP Chinese website!