Flask开发实践
[參考資料]:
Flask從入門到做出一個博客的大型教程(一)
Flask從入門到做出一個博客的大型教程(二)
Flask從入門到做出一個博客的大型教程(三)
Flask從入門到做出一個博客的大型教程(四)
Flask從入門到做出一個博客的大型教程(五)
我跟隨上面的教程進行了實踐,原作者(程序員duke)寫的非常詳細!
與原教程不一致的地方:
1.使用的是PostgreSQL數據庫
對應的修改為:
插件安裝:pip install psycopg2
連接串:postgresql://username:password@localhost:5432/db_name
2.用戶頭像使用了靜態資源
因為國內訪問Gravatar不穩定,所以放棄了使用在線圖片(URL)的方法。
將用到的靜態圖片資源放在/app/static目錄下,因為app目錄是更目錄,并且Flask(應該也是采用了常見的“約定大于配置”的方案)所以要求靜態資源的路徑必須是static。
routes.py寫法
# 用戶資料 @webapp.route('/user/<username>') @login_required def user(username):tmp_user = User.query.filter_by(username=username).first_or_404()posts = [{'author': tmp_user, 'body': '測試Post #1號'},{'author': tmp_user, 'body': '測試Post #2號'}]return render_template('user.html', user=tmp_user, posts=posts, file='/static/WALL-E.jpg')對應的HTML寫法
<td><img src="{{ file }}"></td>【PS】還有一種使用圖片流的做法,暫時沒有深究,記錄如下:
models.py中的寫法
對應的HTML寫法
<td><img src="{{ post.author.return_img_stream() }}"></td>完整的項目代碼位置:
https://github.com/chenth0517/urination_record.git
支持Git clone,也支持SVN checkout
總結
- 上一篇: PostgreSQL和Excel的数据合
- 下一篇: Apache24 + wsgi + Fl