Python flask-paginate在页面上返回太多结果

时间:2016-04-24 23:16:44

标签: python flask pagination

我正在使用模块flask-paginate:http://pythonhosted.org/Flask-paginate/

我可以返回结果,我的分页从正确的页数开始,但它目前每页显示69个结果。文档显示per_page =,但这只会影响我的起始页码,这是正确的。我使用SQL-Alchemy作为我的数据库。

@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

                      )

3 个答案:

答案 0 :(得分:0)

如果您打印出page,您可能会看到它始终返回1,因为您尝试访问其范围之外的request,搜索路由功能和{{ 1}}总是返回.get()

尝试这样的事情......

1

如果您想继续使用单独的功能,则需要将@search.route('/search') def search(): ... page = int(request.args.get('page', 1)) per_page = 10 inner_window = 10 offset = page * 10 ... 对象传递给它才能访问这些值。

希望这有帮助!

答案 1 :(得分:0)

我能够通过Flask-Paginate从数据库查询的links = Item.query.paginate(page, 10, True) 切换到只通过Flask-Sqlalchemy的SQLALchemy来解决这个问题。

我将其更改为每页返回所需的结果..

Private _ExchangeServicesMainThread As New Dictionary(Of String, ExchangeService)
Private _UserIdState As New Dictionary(Of String, Integer)
Private _Connections As New Dictionary(Of String, StreamingSubscriptionConnection)
Private _Subscriptions As New Dictionary(Of String, StreamingSubscription)

Private Function InitializeService(pUsername As String) As ExchangeService
    Dim oExService As ExchangeService
    Dim strEmailAddress As String = pUsername & config.MailDomain
    Dim strAdminEmailAddress As String = config.ExchangeServerAdminUserName & config.MailDomain

        oExService = New ExchangeService(config.ExchangeVersion)
        oExService.UseDefaultCredentials = False
        oExService.Url = New Uri(config.SecureHTTP & config.EmailServer & config.ExchangeManagedAPIEndpoint)
        oExService.Credentials = New Net.NetworkCredential(config.ExchangeServerAdminUserName, config.ExchangeServerAdminPass, config.ActiveDirectoryDomain)
        oExService.ImpersonatedUserId = New ImpersonatedUserId(ConnectingIdType.SmtpAddress, strEmailAddress)

        Return oExService
End Function

更多信息为here (SQLAlchemy and paging)`Flask-SqlAlchemy docs'

答案 2 :(得分:0)

我看到您选择使用Flask-SQLAlchemy,但希望这个答案可以帮助其他人:

你几乎就在那里 - 你正确地捕获了page,per_page和offset(实际上有一个built-in,你可以使用from flask_paginate import get_page_args导入),但是你还需要使用per_page和您的查询的偏移量。所以在你的例子中:

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

以下是一个完整的例子:

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)