【发布时间】: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 也用作查询字符串参数。我还在路由中添加了<q>,因为该视图函数的第一行是q = request.args.get('q')。但是当我做这个改变时,它给了我一个 BuildError 如下:
werkzeug.routing.BuildError
BuildError: ('showAllPuppies', {'shelter_id': 2}, None)
【问题讨论】:
标签: python pagination flask-sqlalchemy