1

Im using the module flask-paginate found here: http://pythonhosted.org/Flask-paginate/

I am able to return results, and my pagination begins with the correct number of pages, but its currently showing 69 results per page. The documentation shows per_page= , but this only affects my starting page number which is correct. Im using SQL-Alchemy for my db.

@search.route('/search')
def search():

    page, per_page, offset, inner_window = get_page_items()

    links = Item.query.all()
    total = Item.query.count()
    pagination = get_pagination(page=page,
                            per_page=per_page,


                            total = total,        
                            format_total=True,  
                            format_number=True,  
                            record_name='links',

                            )

    return render_template('search/searchPage.html', offset=offset, total=total, links=links, pagination=pagination, per_page=per_page, page=page)

def get_css_framework():
    return 'bootstrap3'

def get_link_size():
    return 'sm'  #option lg

def show_single_page_or_not():
    return False

def get_page_items():
    page = int(request.args.get('page', 1))
    per_page = 10

    inner_window=10

    offset = (page) * 10
    return page, per_page, offset, inner_window


def get_pagination(**kwargs):
    kwargs.setdefault('record_name', 'repositories')

    return Pagination(css_framework=get_css_framework(),
                      link_size=get_link_size(),
                      show_single_page=show_single_page_or_not(),

                      **kwargs

                      )
Anekdotin
  • 1,531
  • 4
  • 21
  • 43

3 Answers3

0

If you print out page you will probably see that it always returns 1 since you are trying to access the request outside of its scope, the search route function, and .get() is always returning 1.

Try something like this...

@search.route('/search')
    def search():
        ...

        page = int(request.args.get('page', 1))
        per_page = 10
        inner_window = 10
        offset = page * 10

        ...

If you want to continue to use a separate function the you will need to pass the request object to it in order to access the values.

Hope that this helps!

siegerts
  • 461
  • 4
  • 11
  • still 69 per page :( – Anekdotin Apr 25 '16 at 21:49
  • I would start with the base implementation that they show in the docs and the begin to abstract out functionality if you want once you know that its working. Can you edit to include your view and your db set up? – siegerts Apr 26 '16 at 13:39
0

I was able to solve this by switching from Flask-Paginate for the DB query to just SQLALchemy via Flask-Sqlalchemy.

I changed this to return desired results per page..

links = Item.query.paginate(page, 10, True)

More information is here (SQLAlchemy and paging) and on the `Flask-SqlAlchemy docs'.

Community
  • 1
  • 1
Anekdotin
  • 1,531
  • 4
  • 21
  • 43
0

I see that you opted to use Flask-SQLAlchemy instead, but hopefully this answer will help someone else:

You were almost there - you correctly captured page, per_page, and offset (there's actually a built-in for this, which you can import using from flask_paginate import get_page_args), but you also need to use the per_page and offset on your query. So in your example:

all_links = Item.query.all()
links_for_render_template = Item.query.limit(per_page).offset(offset)

Here's a full example as well:

from flask_paginate import Pagination, get_page_args

@search.route('/search')
def search():
    page, per_page, offset = get_page_args()
    all_links = Item.query.all()
    links_for_render_template = Item.query.limit(per_page).offset(offset)

    pagination = get_pagination(page=page, per_page=per_page, offset=offset, total=all_links.count(), record_name='links')

    return render_template('search/searchPage.html', links=links_for_render_template, pagination=pagination)
ZaxR
  • 4,896
  • 4
  • 23
  • 42