Python 烧瓶分页示例无法工作缺少“;国际热核实验堆“页数”;

Python 烧瓶分页示例无法工作缺少“;国际热核实验堆“页数”;,python,pagination,flask,sqlalchemy,Python,Pagination,Flask,Sqlalchemy,我正在开发一个小的web应用程序来查看一些日志文件。但是 我向数据库发出的查询会变得非常大 我想按照这个例子实现一些分页。 我将该类放入一个文件中,该文件将加载到Flask视图中。接下来我实现了 我的分页视图如下所示: @app.route('/index/', defaults={'page':1}) @app.route('/index/page/<int:page>') def index(page): count = db_session.execute("selec

我正在开发一个小的web应用程序来查看一些日志文件。但是 我向数据库发出的查询会变得非常大

我想按照这个例子实现一些分页。 我将该类放入一个文件中,该文件将加载到Flask视图中。接下来我实现了 我的分页视图如下所示:

@app.route('/index/', defaults={'page':1})
@app.route('/index/page/<int:page>')
def index(page):
    count = db_session.execute("select host,facility,level,msg from messages").rowcount
    tblqry = db_session.execute("select host,facility,level,msg from messages").fetchmany(size=1000)
    if not tblqry and page != 1:
        abort(404)
    pagination = Pagination(page, PER_PAGE, count)
    return render_template('index.html', pagination=pagination, tblqry=tblqry)
{{ render_pagination(host[0]) }}
但当我尝试这样做时:

@app.route('/index/', defaults={'page':1})
@app.route('/index/page/<int:page>')
def index(page):
    count = db_session.execute("select host,facility,level,msg from messages").rowcount
    tblqry = db_session.execute("select host,facility,level,msg from messages").fetchmany(size=1000)
    if not tblqry and page != 1:
        abort(404)
    pagination = Pagination(page, PER_PAGE, count)
    return render_template('index.html', pagination=pagination, tblqry=tblqry)
{{ render_pagination(host[0]) }}
他声称:

UndefinedError: 'str object' has no attribute 'iter_pages'
那么,为什么flask无法找到“iter_页面”,因为我在视图文件中包含了分页类

而且,我也不确定如何将URL生成帮助器放在哪里

编辑: 这就是我的分页帮助器的外观:

{% macro render_pagination(pagination) %}
  <div class=pagination>
  {% for page in pagination.iter_pages() %}
    {% if page %}
      {% if page != pagination.page %}
        <a href="{{ url_for_other_page(page) }}">{{ page }}</a>
      {% else %}
        <strong>{{ page }}</strong>
      {% endif %}
    {% else %}
      <span class=ellipsis>…</span>
    {% endif %}
  {%- endfor %}
  {% if pagination.has_next %}
    <a href="{{ url_for_other_page(pagination.page + 1)}}">Next &raquo;</a>
  {% endif %}
  </div>
{% endmacro %}
{%macro render_分页(分页)%}
{%用于分页中的页面。iter_pages()%}
{%if page%}
{%if page!=pagination.page%}
{%else%}
{{page}
{%endif%}
{%else%}
…
{%endif%}
{%-endfor%}
{%if pagination.has_next%}
{%endif%}
{%endmacro%}

您需要将
分页
对象传递给宏,而不是字符串<代码>主机[0]是一个字符串,而不是在视图函数中创建的
分页值

使用:


您需要将
分页
对象传递给宏,而不是字符串<代码>主机[0]
是一个字符串,而不是在视图函数中创建的
分页值

使用:


您需要将
分页
对象传递给宏,而不是字符串<代码>主机[0]
是一个字符串,而不是在视图函数中创建的
分页值

使用:


您需要将
分页
对象传递给宏,而不是字符串<代码>主机[0]
是一个字符串,而不是在视图函数中创建的
分页值

使用:


您能给我们看一下您的
\u pagination\u helper.html
code吗?@SyedHabibM:我想它和中的一样。@SyedHabibM shure我能,但Martijn Pieters是对的,它与中引用的一样。您能给我们看一下您的
\u pagination\u helper.html
code吗?@SyedHabibM:我想它和中的一样。@SyedHabibM舒尔我可以,但Martijn Pieters是对的,它与烧瓶片段中引用的内容相同。您可以向我们展示您的
\u pagination\u helper.html
代码吗?@SyedHabibM:我想它与中的内容相同。@SyedHabibM舒尔我可以,但Martijn Pieters是对的,它与烧瓶片段中引用的内容相同,您可以向我们展示您的
\u pagination\u helper.html
code?@SyedHabibM:我想它与中的相同。@SyedHabibM shure我可以,但Martijn Pieters是对的,它与中引用的相同。该脚本对第一页(即第二页)执行限制1,20,这是错误的。那么零限制0,1呢?这个脚本对第一页(即第二页)执行限制1,20,这是错误的。那么零限制0,1呢?这个脚本对第一页(即第二页)执行限制1,20,这是错误的。那么零限制0,1呢?这个脚本对第一页(即第二页)执行限制1,20,这是错误的。零限制0,1怎么样?