flask_sqlalchemy对象提供分页方法
1. 后台views代码:
from models import <table_name> #导入model的对象 @app.route('/', methods=['GET', 'POST']) @app.route('/<int:page>', methods=['GET', 'POST']) def index(page=1): pagination = <table_name>.query.paginate(page, 20, False) #flask_sqlalchemy提供分页方法.paginate db.session.close() return render_template('index.html', pagination=pagination)
2.模板页面:
<div class="pagination"> <ul class="pagination"> {% for page in pagination.iter_pages() %} <li> <a class="page-link3" href="{{ url_for('index', page=page) }}">{{ page }}</a> </li> {% endfor %} </ul> </div>
3. javascript/Ajax 请求代码:
$(document).on('click', '.page-link3', function(event) { var link = $(this).attr('href'); $.ajax({ type:'POST', url:link, success:function(data){ $("body").html(data); } }); event.preventDefault(); }); });