1.對(duì)比Django
比較項(xiàng) | Django | Flask | 復(fù)雜度比較 | 說明 |
---|---|---|---|---|
項(xiàng)目創(chuàng)建 | Django需要用命令創(chuàng)建項(xiàng)目 | Flask直接編寫文件就可以運(yùn)行 | Django復(fù)雜 | Django需要用命令創(chuàng)建項(xiàng)目是因?yàn)樾枰獎(jiǎng)?chuàng)建整個(gè)項(xiàng)目框架 |
路由 | Django使用專門的urls.py | Flask直接使用@app.route() | Django笨重 | Django類似Struct2的配置崖叫,F(xiàn)lask類似Spring的配置纫骑,F(xiàn)lask感覺更好 |
GET和POST | request.GET['name']和request.POST['name'] | request.args.get("name","")和request.form["q"] | 差不多 | Flask上格式不統(tǒng)一 |
Restful | 使用django-restful框架 | 使用flask-restful框架 | 差不多 | Flask使用方法flask-restfu使用類格式不統(tǒng)一;flask-restful需要注冊(cè)參數(shù) |
數(shù)據(jù)庫操作 | django集成了對(duì)數(shù)據(jù)庫的操作 | Flask沒集成對(duì)數(shù)據(jù)庫的操作要另行 | 差不多 | django復(fù)雜很大程度來源于對(duì)于數(shù)據(jù)庫的集成 |
Django繼承了MVT和ORM,以及后臺(tái)管理。
Flask兩個(gè)主要核心應(yīng)用是EWerkzeug和模板引擎jinja2。
擴(kuò)展包
Flask | Django | ||
---|---|---|---|
Flask-SQLalchemy | 操作數(shù)據(jù)庫 | django-admin | |
Flask-migrate | 管理遷移數(shù)據(jù)庫 | manage.py | |
Flask-Mail | 郵件 | orm模型 | |
Flask-WTF | 表單 | admin | |
Flask-script | 插入腳本 | 文件存儲(chǔ)系統(tǒng) | |
Flask-OpenID | 認(rèn)證 | 用戶認(rèn)證系統(tǒng) | |
Flask-Login | 認(rèn)證用戶狀態(tài) | ||
Flask-RESTful | 開發(fā)REST API的工具 | ||
Flask-Bootstrap | 集成前端Twiiter Bootstrap框架 | ||
Flask-Moment | 本地化日期和時(shí)間 |
Django內(nèi)容 Flsak都沒有需要擴(kuò)展包提供 框架核心 : Werkzerug + jinja2(輕)
2.安裝
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="shell" cid="n103" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip3 install flask</pre>
第一個(gè)項(xiàng)目
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n106" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># --UTF-8--
-lv wq --
導(dǎo)入Flask類
from flask import Flask
Flask類接收一個(gè)參數(shù)name
app = Flask(name)
裝飾器的作用是將路由映射到視圖函數(shù)index
@app.route('/')
def index():
return "hello,world"
Flask 應(yīng)用程序?qū)嵗膔un方法啟動(dòng)web服務(wù)器
if name == 'main':
app.run()</pre>
Flask對(duì)象的初始化參數(shù)
應(yīng)用程序配置參數(shù)
app.run()運(yùn)行參數(shù)
應(yīng)用程序配置參數(shù)
Django將所有配置信息都放到了settings.py文件中,而Flask則不同楞卡。
Flask將配置信息保存到了app.config 屬性中,該屬性可以按照字典類型進(jìn)行操作宛琅。
讀取
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n115" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from flask import Flask
app = Flask(name)
app.config.get(name)
app.config[name]</pre>
設(shè)置
1.從配置對(duì)象中加載
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n118" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">app.config.from_object(配置對(duì)象)</pre>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n119" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">class DefaultConfig(object):
"""
默認(rèn)配置
"""
SECRET_KEY = 'TPmi4aLWRbyVq8zu9v82dWYW1'
app = Flask(name)
app.config.from_object(DefaultConfig)
@app.route("/")
def index():
print(app.config['SECRET_KEY'])
return 'HELLO World'
應(yīng)用場(chǎng)景刻蟹,可以繼承---------------------------------------
class DevelopmentConfig(DefaultConfig):
DEBUG=True</pre>
例子:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n121" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># --UTF-8--
-lv wq --
導(dǎo)入Flask類
from flask import Flask
class DefaultConfig(object):
"""默認(rèn)配置"""
SECRET_KEY = 'CHDWDIJQIDQWDQW123DKWENFF33'
Flask類接收一個(gè)參數(shù)name
app = Flask(name)
app.config.from_object(DefaultConfig) #
裝飾器的作用是將路由映射到視圖函數(shù)index
@app.route('/')
def index():
print(app.config['SECRET_KEY'])
return "hello,world"
Flask 應(yīng)用程序?qū)嵗膔un方法啟動(dòng)web服務(wù)器
if name == 'main':
app.run()</pre>
2.從配置文件中加載
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n123" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">app.config.from_pyfile(配置文件)</pre>
新建一個(gè)配置文件setting.py
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="python" cid="n125" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SECRET_KEY = 'TPmi4aLWRbyVq8zu9v82dWYW1'</pre>
在Flask程序文件中
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n127" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">app = Flask(name)
app.config.from_pyfile('setting.py')
@app.route("/")
def index():
print(app.config['SECRET_KEY'])
return "hello world"</pre>
例子:
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n129" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># --UTF-8--
-lv wq --
導(dǎo)入Flask類
from flask import Flask
app = Flask(name)
app.config.from_pyfile('settings.py')
裝飾器的作用是將路由映射到視圖函數(shù)index
@app.route('/')
def index():
print(app.config['SECRET_KEY'])
return "hello,world"
Flask 應(yīng)用程序?qū)嵗膔un方法啟動(dòng)web服務(wù)器
if name == 'main':
app.run()</pre>
3.從環(huán)境變量中加載
Flask使用環(huán)境變量加載配置的本質(zhì)是通過環(huán)境變量值找到配置文件,再讀取配置文件的信息嘿辟,其使用方式為
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n134" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">app.config.from_envvar('環(huán)境變量名') # 環(huán)境變量值為配置文件的絕對(duì)路徑 </pre>
先在終端中執(zhí)行如下命令
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n136" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">export PROJECT_SETTING = '~/setting.py'</pre>
再運(yùn)行代碼
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n138" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">app = Flask(name)
app.config.from_envvar('PROJECT_SETTING',silent=True)
@app.route("/")
def index():
print(app.config['SECRET_KEY'])
return "hello world"
if name == 'main':
app.run(host="0.0.0.0",port=5000,debug=True) # 指定運(yùn)行的主機(jī)IP端口和是否開啟DEBUG</pre>
表示系統(tǒng)環(huán)境變量中沒有設(shè)置相應(yīng)值時(shí)是否拋出異常 False 表示不安靜的處理舆瘪,沒有值時(shí)報(bào)錯(cuò)通知,默認(rèn)為False,True 表示安靜的處理红伦,即時(shí)沒有值也讓Flask正常的運(yùn)行下去
3.開發(fā)服務(wù)器啟動(dòng)方式
從1.0版本開始 不再需要在 代碼中寫 app.run()
使用命令 flask run
4.路由
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n145" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@app.route("/login")
def view_func():
return "hello world"</pre>
1.查詢路由信息
--命令行方式
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n148" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">flask routes</pre>
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n150" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Endpoint Methods Rule
index GET /
static GET /static/</pre>
--在程序中獲得
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n152" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">print(app.url_map)</pre>
也可以遍歷路由信息
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n154" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">for rule in app.url_map-iter_rules():
print('name = {} path = {}'.format(rule.endpoint,rule.rule))</pre>
2.獲取請(qǐng)求參數(shù)
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n156" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from flask import request</pre>
屬性 | 說明 | 類型 |
---|---|---|
data | 記錄請(qǐng)求數(shù)據(jù)英古,并轉(zhuǎn)換為字符串 | * |
form | 記錄請(qǐng)求中的表單數(shù)據(jù) | MultiDict |
args | 記錄請(qǐng)求中的查詢參數(shù) | MultiDict |
coockies | 記錄請(qǐng)求中的cookie信息 | Dict |
headers | 記錄請(qǐng)求中的報(bào)文頭 | EnvironHeaders |
method | 記錄請(qǐng)求使用的HTTP方法 | GET/POST |
url | 記錄請(qǐng)求的URL地址 | String |
files | 記錄請(qǐng)求上傳的文件 | * |
5.請(qǐng)求鉤子
請(qǐng)求鉤子是通過裝飾器的形式實(shí)現(xiàn),F(xiàn)lask支持四種請(qǐng)求鉤子:
before_first_request : 在處理第一個(gè)請(qǐng)求前運(yùn)行昙读。
before_request : 在每次請(qǐng)求前運(yùn)行
after_request : 如果沒有未處理的異常拋出召调,在每次請(qǐng)求后運(yùn)行。
teatrdown_request : 在每次請(qǐng)求后運(yùn)行蛮浑,即使有未處理的異常拋出
6.JinJa2模板
http://docs.jinkan.org/docs/jinja2/
Flask使用Jinja2這個(gè)模板引擎來渲染模板唠叛。Jinja2能識(shí)別所有類型的變量,包括{}沮稚。 Jinja2模板引擎艺沼,F(xiàn)lask提供的render_template函數(shù)封裝了該模板引擎,render_template函數(shù)的第一個(gè)參數(shù)是模板的文件名蕴掏,后面的參數(shù)都是鍵值對(duì)障般,表示模板中變量對(duì)應(yīng)的真實(shí)值
7.Web表單
web表單是web應(yīng)用程序的基本功能
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n207" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip3 install Flask-WTF</pre>
WTForms支持的HTML標(biāo)準(zhǔn)字段
字段對(duì)象 | 說明 |
---|---|
StringField | 文本字段 |
TextAreaField | 多行文本字段 |
PasswordField | 密碼文本字段 |
HiddenField | 隱藏文本字段 |
DateField | 文本字段,值為datetime.date格式 |
DateTimeField | 文本字段盛杰,值為datetime.datetime格式 |
IntegerField | 文本字段挽荡,值為整數(shù) |
DecimalField | 文本字段,值為decimal.Decimal |
FloatField | 文本字段饶唤,值為浮點(diǎn)數(shù) |
BooleanField | 復(fù)選框徐伐,值為True和False |
RadioField | 一組單選框 |
SelectField | 下拉列表 |
SelectMultipleField | 下拉列表,可選擇多個(gè)值 |
FileField | 文本上傳字段 |
SubmitField | 表單提交按鈕 |
FormField | 把表單作為字段嵌入另一個(gè)表單 |
FieldList | 一組指定類型的字段 |
8.數(shù)據(jù)庫
flask-sqlalchemy是一個(gè)簡(jiǎn)化了SQLAlchemy操作的flflask擴(kuò)展募狂。
1.安裝服務(wù)端
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n267" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">sudo apt-get install mysql-server</pre>
2.安裝客戶端
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n269" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">sudo apt-get install mysql-client
sudo apt-get install libmysqlclient-dev</pre>
3.數(shù)據(jù)庫的基本命令
登錄數(shù)據(jù)庫
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n272" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">mysql -u root -p</pre>
創(chuàng)建數(shù)據(jù)庫办素,設(shè)定編碼
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="sql" cid="n274" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">create database <數(shù)據(jù)庫名> charset=utf8;</pre>
顯示所有數(shù)據(jù)庫
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="sql" cid="n276" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">show databases;</pre>
在Flask中使用mysql數(shù)據(jù)庫,需要安裝一個(gè)flask-sqlalchemy的擴(kuò)展
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n278" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install flask-sqlachemy</pre>
4.對(duì)比Django和Flask數(shù)據(jù)庫設(shè)置
Django :
DATABASES = { 'default': { 'ENGINE': None, # 把默認(rèn)的數(shù)據(jù)庫連接至為None } }
Flask:
app.confifig['SQLALCHEMY_DATABASE_URI'] ="************"
5.數(shù)據(jù)庫遷移
創(chuàng)建遷移倉庫
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n286" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">python database.py db init</pre>
創(chuàng)建遷移腳本
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n288" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#創(chuàng)建自動(dòng)遷移腳本
python database.py db migrate -m 'initial migration'</pre>
更新數(shù)據(jù)庫
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n290" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">python database.py db upgrade</pre>
回退數(shù)據(jù)庫
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="shell" cid="n292" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">python database.py db downgrade 版本號(hào)</pre>
9.藍(lán)圖
藍(lán)圖就是一個(gè)存儲(chǔ)操作路由映射方法的容器祸穷,主要用來實(shí)現(xiàn)客戶端請(qǐng)求和URL相互關(guān)聯(lián)的功能性穿。 在Flask中,使用藍(lán)圖可以幫助我們實(shí)現(xiàn)模塊化應(yīng)用的功能雷滚。
1.創(chuàng)建藍(lán)圖對(duì)象
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n296" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#Blueprint必須指定兩個(gè)參數(shù)需曾,admin表示藍(lán)圖的名稱,name表示藍(lán)圖 所在模塊
admin = Blueprint('admin',name)</pre>
2.注冊(cè)藍(lán)圖路由
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n298" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@admin.route('/')
def admin_index():
return 'admin_index'</pre>
3.在程序?qū)嵗?cè)路由
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n300" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">app.register_blueprint(admin,url_prefix='/admin')</pre>
實(shí)例:
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="python" cid="n302" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from flask_restful import Api
api = Api()
def init_api(app): # 傳進(jìn)來的app要繼承過數(shù)據(jù)庫配置DbConfig
api.init_app(app)
api.add_resource(class, "")</pre>
10.單元測(cè)試
常用斷言方法
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="" cid="n305" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">assertEqual 如果兩個(gè)值相等,則pass
assertNotEqual 如果兩個(gè)值不相等呆万,則pass
assertTrue 判斷bool值為True商源,則pass
assertFalse 判斷bool值為False,則pass
assertIsNone 不存在谋减,則pass assertIsNotNone 存在牡彻,則pass</pre>