17flask分页
一,flask_sqlachemy的使用
如果想要展示出来的页面是分页显示,则首先需要知道每页应该分多少个条目,然后通过数据库去查找对应的条数,同时也需要和分页所需的“paginate”结合使用。
先贴代码:
1 @app.route('/') 2 def index(): 3 # 原始代码 4 # context = { 5 # 'questions':Question.query.order_by('create_time').all() #注意all() 6 # } 7 # # print(context) 8 # return render_template('index.html',**context) #**的使用 9 # print(request.args.get('page')) 10 11 #分页代码 12 # print(request.args.get('page',1)) 13 page = int(request.args.get('page', 1)) 14 per_page = int(request.args.get('per_page', 3)) 15 # 上面两行别丢了,不能用下面的下,不然不能知道当前页了。 16 #page = 1 17 #per_page = 3 18 19 paginate = Question.query.order_by(db.desc(Question.create_time)).paginate(page, per_page, error_out=False) 20 #这行代码首先根据“db.desc(Question.create_time)“按照创建时间拍个序号, 21 #然后“.paginate(page, per_page, error_out=False)”就成,没啥特殊操作。 22 questions = paginate.items 23 # 将项目与paginate对应 24 25 question_max_id = Question.query.order_by(db.desc(Question.id)).first().id 26 27 context = { 28 'paginate': paginate, 29 'questions': questions, 30 'question_max_id': question_max_id 31 #这里我加入了一个最大序号,是为了将最新的条目加“new”标记。 32 } 33 34 return render_template('index.html', **context) # **的使用。
原始代码是没有分页的情况。
先看看分页代码中的paginate的用法:
paginate(page, per_page, error_out=True)
page 当前页数
per_page 每页显示的条数
error_out 是否打印错误信息,Flase表页码超出后返回空,否则返回404.
二,jinja2显示
先贴代码
1 {% extends 'base.html' %} 2 {% block title %}首页{% endblock %} 3 4 {% block head %} 5 "UTF-8"> 6 "stylesheet" href="{{ url_for('static',filename = "css/index.css") }}"> 7 {% endblock %} 8 9 {% block main %} 10
-
11 {% for question in questions %}
12
13
-
14 class="question-list-group"> 1517 18
"{{ url_for('static',filename = "images/dajun1.png")}}" alt="" class="avatar"> 16
class="question-group"> 19 2049class="question-title"> 21 {# "{{ url_for('detail',question_id = question.id) }}">{{ question.title }}#} 22 {# 如果当前的问题id大于==id,则给它一个new标签,否则就按照常规输出#} 23 {% if question.id == question_max_id %} 24 {#
{{ question.title }} class="label label-default">New
#} 25 "{{ url_for('detail',question_id = question.id) }}">{{ question.title }} class="label label-default">New 26 {% else %} 27 "{{ url_for('detail',question_id = question.id) }}">{{ question.title }} 28 {% endif %} 29 30 {# #这里注意下带其他参数的question_id的用法#} 31 32 33class="question-content" > 34 {# 这里的使得把形如“
打撒
”这样的文本按照样式显示出来。#} 35 {{ question.content|safe }} 36 37 38class="question-info"> 39 class="question-author"> 40 "{{ url_for('other_user_info',anwser_name =question.author.username) }}">{{ question.author.username }} 41 42 43 class="question-time"> 44 {{ question.create_time }} 45 4647 48
50
51 {% endfor %}
52
53 {# 当前页数:{{ paginate.page }}#}
54 {# 总页数:{{ paginate.pages }}#}
55 {# 一共有{{ paginate.total }}条数据#}
56 {#
#} 57 58 {# {% if paginate.has_prev %}#} 59 {# "?page={{ paginate.prev_num }}">上一页#} 60 {# {% endif %}#} 61 {# 页码:#} 62 {##} 63 {# {% for i in paginate.iter_pages() %}#} 64 {# "?page={{ i }}">{{ i }}#} 65 {# {% endfor %}#} 66 {##} 67 {# {% if paginate.has_next %}#} 68 {# "?page={{ paginate.next_num }}">下一页#} 69 {# {% endif %}#} 70 {#
139
行1:引用模板。
行9--行149是block块。
行10--51显示一中的行29内的内容。
行22--28,把最新的条目加“new”标记。
行73的代码能用,是只有前页和后页的情况。
行93--106当前页面为1,则前翻disable。行116同。
注意:行109中是“for i in paginate.iter_pages()”而非“paginate.pages”,不然会显示“TypeError: 'int' object is not iterable”。
行140是翻页进度条,这是据下载条改的,bootstrap都有。