Python Flask SQLAlchemy 分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43103585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Flask SQLAlchemy Pagination
提问by mechanical_meat
I am having trouble implementing pagination with Flask-SQLAlchemy or Flask-Pagination, either or. I am unsure how to initialize the pagination, setting pages, determining pages, offest, etc. I am coming from PHP, quite new to Python.
我在使用 Flask-SQLAlchemy 或 Flask-Pagination 实现分页时遇到问题,或者。我不确定如何初始化分页、设置页面、确定页面、offest 等。我来自 PHP,对 Python 非常陌生。
I am querying all the posts in my database
我正在查询数据库中的所有帖子
posts = Posts.query.order_by(Posts.time.desc()).all()
I have been looking at the following examples:
我一直在查看以下示例:
- http://www.ergo.io/tutorials/better-pagination-in-flask/better-pagination-in-flask/
- https://pythonhosted.org/Flask-paginate/
- sqlalchemy pagination
- http://www.ergo.io/tutorials/better-pagination-in-flask/better-pagination-in-flask/
- https://pythonhosted.org/Flask-paginate/
- sqlalchemy 分页
I am confused on really what to do, the information I am finding greatly differs between articles. It's left me with confusion and not knowing where to begin. I want to query all rows of the database table, limit the results to 20 and paginate. I'm not seeing this clearly.
我真的不知道该怎么做,我发现的信息在文章之间有很大的不同。这让我很困惑,不知道从哪里开始。我想查询数据库表的所有行,将结果限制为 20 并进行分页。我没看清楚。
回答by mechanical_meat
I recommend using Flask-SQLAlchemy's pagination: http://flask-sqlalchemy.pocoo.org/2.1/api/?highlight=pagination#flask.ext.sqlalchemy.Pagination
我建议使用 Flask-SQLAlchemy 的分页:http: //flask-sqlalchemy.pocoo.org/2.1/api/?highlight=pagination# flask.ext.sqlalchemy.Pagination
There's a well-written example here: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ix-pagination
这里有一个写得很好的例子:https: //blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ix-pagination
Here's the basic idea for the view:
这是视图的基本思想:
@app.route('/myview/<int:page>',methods=['GET'])
def view(page=1):
per_page = 10
posts = Posts.query.order_by(Posts.time.desc()).paginate(page,per_page,error_out=False)
return render_template('view.html',posts=posts)
And then for the template (I don't know your posts model so I made something up):
然后是模板(我不知道你的帖子模型,所以我做了一些):
<html>
<head>
Posts
</head>
<body>
{% for post in posts.items %}
<p>
{{ post.post_name }} post body: <b>{{ post.body }}</b>
</p>
{% endfor %}
{% if posts.has_prev %}<a href="{{ url_for('view', page=posts.prev_num) }}"><< Newer posts</a>{% else %}<< Newer posts{% endif %} |
{% if posts.has_next %}<a href="{{ url_for('view', page=posts.next_num) }}">Older posts >></a>{% else %}Older posts >>{% endif %}
</body>
</html>
回答by user3186184
Controller
控制器
@app.route('/', methods=['GET'], defaults={"page": 1})
@app.route('/<int:page>', methods=['GET'])
def index(page):
page = page
per_page = 2
users = User.query.paginate(page,per_page,error_out=False)
# print("Result......", users)
return render_template("index.html", users=users)
in the View
在视图中
{% for user in users.items %}
<h1> {{ user.name }} </h1>
{% endfor %}
<nav aria-label="Page navigation example">
<ul class="pagination">
{% if users.has_prev %}
<li class="page-item"> <a class="page-link" href="{{ url_for('index', page=users.prev_num) }}">Previous</a></li>
{% else %}
<li class="page-item"><a class="page-link btn disabled" href="#">Previous</a></li>
{% endif %}
{% if users.has_next %}
<li class="page-item"> <a class="page-link" href="{{ url_for('index', page=users.next_num) }}">Next</a></li>
{% else %}
<li class="page-item"><a class="page-link btn disabled" href="#">Next</a></li>
{% endif %}
</ul>
</nav>