Home  >  Article  >  Backend Development  >  Detailed introduction to flask paging

Detailed introduction to flask paging

零下一度
零下一度Original
2017-06-27 09:53:082030browse

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(&#39;/<int:page>&#39;)
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(&#39;home1.html&#39;,
                           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=&#39;background-color: lightgoldenrodyellow;color: brown;&#39; 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=&#39;background-color: lightgoldenrodyellow;color: brown;&#39; href="{{ url_for(endpoint, page = p, **kwargs) }}">{{ p }}</a>
</li>
{% else %}
<li>
<a style=&#39;background-color: lightgoldenrodyellow;color: brown;&#39; 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=&#39;background-color: lightgoldenrodyellow;color: brown;&#39; 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 shown

The above is the detailed content of Detailed introduction to flask paging. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn