这周主要完成了数据库表建立到flaskapp搭建到前段页面实现css
jizhuan static css image js favicon.ico templates all.html index.html order_confirm.html order_list.html __ini__.py models.py view.py run.py
###数据库访问 咱们使用了MySQL存了一张数据表html
在models.py,用SQLAlchemy定义了数据库表单模型,以及读取数据的几个方法python
关于Flask-SQLAlchemy的使用,参考快速入门sql
访问查询数据库的一些方法:数据库
#返回全部用户保存到list中 info_list = Sheet_form.query.all() #查找username为abc的第一个用户,返回用户实例 info_abc = Sheet_form.query.filter_by(username='abc').first() #模糊查找用户名以c结尾的全部用户 info_c = Sheet_form.query.filter(username.endswith('c')).all() #查找用户名不是abc的用户 info_nabc = User.query.filter(username != 'abc').first()
因为数据量比较多(400多条),就借助Flask-SQLALchemy中的的Pagination对象进行分页处理。分页查询的方法:flask
def select_paginate(page): try: pagination = Sheet_Form.query.paginate(page, per_page=6, error_out = False) return pagination except IOError: return None return None
pagenate(page, per_page=6, error_out=True)方法能够返回一个查询的pagination对象,第一个参数表示当前页,第二个参数表明每页显示的数量,error_out=True的状况下若是指定页没有内容将出现404错误,不然返回空的列表。bootstrap
pagination对象经常使用方法有:app
这些方法会在后面的模板中用到。函数
###路由 在view.py中规定了url的绑定,主要有3个:学习
from models import select_all from models import select_paginate @app.route('/') def blank(): pagination = select_paginate(1) return render_template('index.html',title= '三螺旋', pagination= pagination) @app.route('/all') def all(): info_all = select_all() return render_template('all.html',title='三螺旋',form=info_all) @app.route('/index/<int:page>') def company(page): pagination = select_paginate(page) return render_template('index.html',title= "三螺旋",pagination=pagination)
其中company方法调用到了分页的方法,每页6条数据,根据参数page获取该页pagination并传递到模板文件中。
###模板 模板使用了Flask-Bootstrap,参考基本用法,主要解决了将pagination对象的数据显示以及分页导航栏的显示:
<div class="row-fluid"> {% for f in pagination.items %} {% if f.id %} <div class="col-md-3 col-sm-3 col-xs-3 panel panel-default panel-user"> <h3>{{ f.company }}</h3> <div class="user-info-university"> <span><a>{{ f.url }}</a></span> </div> <div class="user-info-subject"> <span>联系电话:{{ f.tel }}</span> </div> <div class="user-info-subject"> <span>传真:{{ f.fax }}</span> </div> <div class="user-info-article"> <span>地址:</span> <span>{{ f.address }}</span> </div> </div> {% endif %} {% endfor %} </div>
分页导航栏的实现:
<nav class="pagination-bottom" style="text-align: center"> <ul class="pagination"> <li{% if not pagination.has_prev %} class="disabled"{% endif %}> <a href="/index/{{ pagination.prev_num }}">«</a> </li> {% for p in pagination.iter_pages() %} {% if p %} {% if p == pagination.page %} <li class="active"> <a href="{{ url_for('company', page = p) }}">{{ p }}</a> </li> {% else %} <li> <a href="{{ url_for('company', page = p) }}">{{ p }}</a> </li> {% endif %} {% else %} <li class="disabled"><a href="#">…</a></li> {% endif %} {% endfor %} <li{% if not pagination.has_next %} class="disabled"{% endif %}> <a href="{% if pagination.has_next %}{{ url_for('company',page=pagination.next_num) }}{% else %}#{% endif %}"> » </a> </li> </ul> </nav>
其中定向到某一页的方法能够直接令标签的href="/index/{{ pagination.prev_num }}",或是用url_for方法,如{{ url_for('company',page=pagination.next_num) }},参数是路由函数的名称及所需参数,在我代码中,就是上面定义的company方法。
###效果 样式放在css文件夹中,最终完成的分页显示数据效果图: