【问题标题】:Python flask-paginate returning too many results on pagePython flask-paginate 在页面上返回太多结果
【发布时间】:2016-08-18 05:16:43
【问题描述】:

我正在使用此处找到的模块 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

                      )

【问题讨论】:

    标签: python flask pagination


    【解决方案1】:

    如果您打印出page,您可能会看到它总是返回1,因为您试图访问其范围之外的request,搜索路由功能,而.get() 总是返回@987654325 @。

    试试这样的...

    @search.route('/search')
        def search():
            ...
    
            page = int(request.args.get('page', 1))
            per_page = 10
            inner_window = 10
            offset = page * 10
    
            ...
    

    如果您想继续使用单独的函数,则需要将 request 对象传递给它才能访问值。

    希望这会有所帮助!

    【讨论】:

    • 仍然是每页 69 个 :(
    • 我会从他们在文档中展示的基本实现开始,如果你知道它的工作原理,如果你愿意,我会开始抽象出功能。您可以编辑以包含您的视图和您的数据库设置吗?
    【解决方案2】:

    我能够通过 Flask-Sqlalchemy 从用于 DB 查询的 Flask-Paginate 切换到仅用于 SQLALchemy 来解决此问题。

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

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

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

    【讨论】:

      【解决方案3】:

      我看到您选择使用 Flask-SQLAlchemy,但希望此答案对其他人有所帮助:

      您快到了 - 您正确捕获了页面、per_page 和偏移量(实际上有一个 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)
      

      【讨论】:

        猜你喜欢
        • 2017-09-26
        • 1970-01-01
        • 2019-03-26
        • 1970-01-01
        • 2014-05-06
        • 2015-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多