【问题标题】:flask-pagination : Number of rows not returned | Pagination doesn't workflask-pagination : 未返回的行数 |分页不起作用
【发布时间】:2016-06-02 06:09:30
【问题描述】:

我无法成功地对记录进行分页。 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 也用作查询字符串参数。我还在路由中添加了&lt;q&gt;,因为该视图函数的第一行是q = request.args.get('q')。但是当我做这个改变时,它给了我一个 BuildError 如下:

werkzeug.routing.BuildError

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

【问题讨论】:

    标签: python pagination flask-sqlalchemy


    【解决方案1】:

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

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

    然后将 total 作为 total=total 传递给 Pagination

    【讨论】:

      猜你喜欢
      • 2015-09-03
      • 2014-12-21
      • 2015-04-20
      • 2015-09-14
      • 2021-01-19
      • 1970-01-01
      • 2018-07-28
      • 2016-06-07
      • 2018-04-06
      相关资源
      最近更新 更多