這周主要完成了數據庫表建立到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文件夾中,最終完成的分頁顯示數據效果圖: