flask-pagination:未返回的行数|分页不起作用

时间:2016-02-03 19:07:07

标签: python pagination flask-sqlalchemy

我无法成功分页记录。 enter image description here This is the link from where I got the flask-pagination help.

我的查看功能如下所示(注意:我只显示了相关代码):

from flask import Blueprint
from flask.ext.paginate import Pagination

mod = Blueprint('runserver', __name__)

@app.route('/home/all-puppies/<int:shelter_id>/<q>')
def showAllPuppies():
    search = False
    q = request.args.get('q')
    if q:
        search = True
    try:
        page = int(request.args.get('page', 1))
    except ValueError:
        page = 1

    puppies = session.query(Puppy, Shelter).join(Shelter).filter(Shelter.id == shelter_id).all()

    pagination = Pagination(page=page, total=puppies.count(puppies), search=search, record_name='puppies')
    return render_template('allpuppies.html',
                           puppies=puppies,
                           pagination=pagination,
                           )

我的 allpuppies.html 看起来像这样:

{% extends "master.html" %}

{% block title %}All Puppies{% endblock %}

{% block body %}
{{ pagination.info|safe }}
{{ pagination.links }}
<table class="table table-condensed" align:"center">
  <thead>
     <tr>
      <th>#</th>
      <th>Name</th>
      <th>Gender</th>
      <th>Weight</th>
      <th>D.O.B</th>
      <th>Shelter</th>
      <th>Location</th>
      <th>Details</th>
    </tr>
  </thead>
  <tbody>
    {% for puppy in puppies %}
      <tr>
        <td>1</td>
         <td>{{ puppy.Puppy.name }}</td>
            <td>{{ puppy.Puppy.gender }}</td>
            <td>{{ puppy.Puppy.weight }}</td>
            <td><b>{{ puppy.Puppy.dateOfBirth }}<b></td>
            <td>{{ puppy.Shelter.name }}</td>
            <td>{{ puppy.Shelter.city }},{{ puppy.Shelter.state}}</td>
            <td><a href='{{ url_for('showPuppyDetails', puppy_id = puppy.Puppy.id ) }}'> Details </a></td>
      </tr>
    {% endfor %}
  </tbody>
</table>
{{ pagination.links }}
{% endblock %}

编辑:1 我更改了查询以仅过滤掉特定避难所中的puppies。为此,我更改了@app.route()以使shelter_id也作为查询字符串参数。我还在路由中添加了<q>,因为该视图函数的第一行是q = request.args.get('q')。但是当我做这个改变时,它给我一个BuildError如下:

  

werkzeug.routing.BuildError

     

BuildError:('showAllPuppies',{'shelter_id':2},无)

1 个答案:

答案 0 :(得分:0)

答案稍晚,但总数是一个单独的查询,可以将模块用作关键字。我会尝试一个单独的查询。

total = puppies = session.query(Puppy, Shelter).join(Shelter).filter(Shelter.id == shelter_id).count()

然后将总数传递给分页总数=总数