我正在尝试使用flask_paginate 对来自后端的数据进行分页。我跟着 https://pythonhosted.org/Flask-paginate/ 来实现它。
我的看法 -
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="utf-8" />
<title>AT - Monitoring System</title>
</head>
<body>
{{ pagination.info }}
{{ pagination.links }}
<table id="myTable">
<thead>
<tr>
<th>Sector ID</th>
<th>Username</th>
<th>Password</th>
<th>Camera Name</th>
<th>Camera ID</th>
<th>Recording status</th>
</tr>
</thead>
<tbody>
{% for each in response %}
<tr>
<td>{{ loop.index + pagination.skip }}</td>
<td>{{each[0]}} </td>
<td>{{each[1]}} </td>
<td>{{each[2]}}</td>
<td>{{each[3]}}</td>
<td> {{each[4]}}</td>
<td>{{each[5]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ pagination.links }}
</body>
</html>
Python代码 -
APP = flask.Flask(__name__)
@APP.route('/', methods=['GET', 'POST'])
def index():
""" Displays the index page accessible at '/'
"""
search = False
q = request.args.get('q')
if q:
search = True
page = request.args.get('page', type = int, default = 1)
pagination = Pagination(page=page, total=len(Backend()), search=search, record_name='response')
if request.method == 'GET':
return flask.render_template('index.html',
response = data,
pagination = pagination
)
if __name__ == '__main__':
data = Backend()
Thread(target=main_loop).start()
APP.debug=True
APP.run(port=63000)
我试图找到显示 pagination.skip() 用法的文档,但找不到任何内容。在删除此功能时,我看到没有。浏览器上的页面数量,但内容未按照页面编号显示。肯定有什么,我错过了很多时间。网上的其他例子和https://pythonhosted.org/Flask-paginate/中提到的例子差别很大
最佳答案
@app.route('/')
def index():
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='per_page')
cursor = mongo.db.NameCollection
urls = cursor.find({}).sort('_id', pymongo.ASCENDING).skip(offset).limit(per_page)
total = cursor.count()
pagination = Pagination(page=page,
per_page=per_page,
total=total,
record_name='users',
format_total=True,
format_number=True,
css_framework='foundation')
return render_template('index.html',
urls=urls,
page=page,
per_page=per_page,
pagination=pagination)
<!DOCTYPE html>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="...">
</head>
<body>
<h1>...</h1>
{{ pagination.info }}
{{ pagination.links }}
<table class="table">
<thead>
<tr>
<th>#</th>
<th>...</th>
<th>...</th>
<th>...</th>
<th>...</th>
</tr>
</thead>
<tbody>
{% for url in urls %}
<tr>
<td>{{ loop.index + (page - 1) * per_page }}</td>
<td>{{ url. ... }}</a></td>
<td>{{ url. ... }}</td>
<td>{{ url. ... }}</td>
<td>{{ url. ... }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ pagination.links }}
</body>
</html>
关于python - 未定义错误 : 'flask_paginate.Pagination object' has no attribute 'skip' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38690043/