librelist archives

« back to archive

Flask-SQLAlchemy pagination: couple of suggestions

Flask-SQLAlchemy pagination: couple of suggestions

From:
Dan Jacob
Date:
2010-07-02 @ 11:08
1. Pagination.iter_pages returns a Page object instead of int.

This would have relevant properties such as page.is_num, page.num,
page.is_current etc., making it a bit easier to access per-page
properties in the template.

2. Additional argument to paginate: page_url, which would be a
callback to generate the URL for each page. Example:

@app.route("/search/")
@app.route("/search/<int:page>/")
def search(page=1):
    search = request.args.get('search', '')
    page_obj = Item.query.search(search).paginate(page, 20,
        page_url=lambda page: url_for(page=page, search=search))
    ....

This would then be rendered in the above Page object as a cached property:

{% for page in page_obj.iter_pages() %}
{% if page.is_num %}
<a href="{{ page.url }}">{{ page }}</a>
....

Re: [flask] Flask-SQLAlchemy pagination: couple of suggestions

From:
DasIch
Date:
2010-07-02 @ 13:44
I like the idea but what is is_num supposed to mean?

Re: [flask] Flask-SQLAlchemy pagination: couple of suggestions

From:
Dan Jacob
Date:
2010-07-02 @ 13:58
From the pagination example in flaskext/sqlalchemy.py:

{% if page %}
      {% if page != pagination.numbers %}

i.e. if the page is skipped or not.

Your Page attributes would more likely be something like:

page.is_first
page.is_last
page.is_current
page.skip
page.url

page.number (str(page) == page.number)





On 2 July 2010 14:44, DasIch <dasdasich@googlemail.com> wrote:
> I like the idea but what is is_num supposed to mean?
>
>