UndefinedError:'flask_paginate.Pagination对象'没有属性'skip'

时间:2016-08-01 02:01:00

标签: python flask paginate

我正在尝试使用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>&nbsp;{{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/

中提到的示例非常不同

1 个答案:

答案 0 :(得分:-2)

@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>