[英]Pagination with PyMongo and Flask
I found here a nice template for a pagination.我在这里找到了一个不错的分页模板。 However, this example was done with SQLlite, but I rewrote in order to be able to use PyMongo.
然而,这个例子是用 SQLlite 完成的,但为了能够使用 PyMongo,我重写了。
This script creates users and save them to MongoDB with help of PyMongo:此脚本在 PyMongo 的帮助下创建用户并将其保存到 MongoDB:
import sys
from pymongo import MongoClient
def fill_data(users_no):
for i in range(users_no):
doc = {
'_id': str(i),
'uname': "name_" + str(i),
}
sDB.insert(doc)
if __name__ == '__main__':
db = MongoClient().test
sDB = db.users
fill_data(1000)
I modified the orginal app.py script in onrder to be able to use PyMongo.我修改了 onrder 中的原始app.py脚本,以便能够使用 PyMongo。 This is the modified script:
这是修改后的脚本:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from __future__ import unicode_literals
from flask import Flask, render_template, g, current_app, request
from flask.ext.paginate import Pagination
from pymongo import MongoClient
app = Flask(__name__)
app.config.from_pyfile('app.cfg')
@app.route('/')
def index():
db_name = "test"
col_name = "users"
db = MongoClient()
sDB = db[db_name][col_name]
total = sDB.find().count()
print total
page, per_page, offset = get_page_items()
users = sDB.find().skip(offset).limit(per_page)
for u in users:
print u
pagination = get_pagination(page=page,
per_page=per_page,
total=total,
record_name=users,
)
return render_template('index.html', users=users,
page=page,
per_page=per_page,
pagination=pagination,
)
def get_css_framework():
return current_app.config.get('CSS_FRAMEWORK', 'bootstrap3')
def get_link_size():
return current_app.config.get('LINK_SIZE', 'sm')
def show_single_page_or_not():
return current_app.config.get('SHOW_SINGLE_PAGE', False)
def get_page_items():
page = int(request.args.get('page', 1))
per_page = request.args.get('per_page')
if not per_page:
per_page = current_app.config.get('PER_PAGE', 10)
else:
per_page = int(per_page)
offset = (page - 1) * per_page
return page, per_page, offset
def get_pagination(**kwargs):
kwargs.setdefault('record_name', 'records')
return Pagination(css_framework=get_css_framework(),
link_size=get_link_size(),
show_single_page=show_single_page_or_not(),
**kwargs
)
if __name__ == '__main__':
app.run(debug=True)
Is there a way to avoid to use count()
by clicking to the next or previous page in the pagination menu?有没有办法通过单击分页菜单中的下一页或上一页来避免使用
count()
?
What did I forget to change so the actual user get be shown in the browser, because currently I only get the pagination menu without users?我忘记更改什么,以便在浏览器中显示实际用户,因为目前我只获得没有用户的分页菜单?
User this link, It w'll help to paginate datas using flask-paginate使用此链接,它将有助于使用flask-paginate对数据进行分页
https://harishvc.com/2015/04/15/pagination-flask-mongodb/ https://harishvc.com/2015/04/15/pagination-flask-mongodb/
if you are not using Flask use:如果您不使用Flask ,请使用:
https://scalegrid.io/blog/fast-paging-with-mongodb/ https://scalegrid.io/blog/fast-paging-with-mongodb/
Hope it will help !希望它会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.