【功能】
基於Flask框架,搭建一套簡單的表格管理平臺。本文爲 第一篇,後續還有。
【前提知識點】
1、瞭解python 主流的web框架
(1)Django:簡單來說就是重武器,是最全能的開發框架,你想要的功能它都有;但是比較繁重,適合企業級的web開發;
(2)Flask:屬於web開發微框架,小巧靈活,相關的第三方庫豐富,適合開發小型web;
(3)Tornado:天生異步,性能強悍,但是框架提供的功能少,需要開發者自己實現;
因此,本此代碼實現主要是基於Flask實現的。
2、瞭解Flask框架
(1)HelloWorld實現:
幾乎所有的編程都是基於“hello world”實現的,因此也大致講下helloworld涉及的內容。
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello world!"
if __name__ == '__main__':
app.run()
先聲明一個Flask框架的對象,然後定義路由'/',即URL地址爲 http://127.0.0.1:5000/;如果定義路徑爲‘/new’,那對應的訪問地址需要改爲http://127.0.0.1:5000/new。另外,@app.route是個裝飾器。
(2)如何實現具體IP地址或者端口的訪問呢?
app.run(debug=True,host="x.x.x.x",port="1234")
通過對app.run()方法的參數定義,分別實現了調試功能,訪問URL變更爲 http://x.x.x.x:1234
這裏調試功能還是很好用的,不但可以幫助開發者重新加載網頁,而且會打印詳細的錯誤信息,協助定位。
(3)如何在web上顯示自己特定的html模板
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('test.html')
if __name__ == '__main__':
app.run()
只需要導入render_template庫,並且在函數返回時改成對應的方法即可。
不過 這 裏要 注意,test.html必須保存在工程目錄與下template文件下,否則會報錯。(這是因爲render_template方法定義時默認寫了template路徑 )
今天就簡單瞭解Flask框架和最基本的helloworld實現方法,下面我將繼續介紹如何實現表格管理平臺。