使用flask作為開發(fā)框架,一定要按功能模塊化,否則到了后面項(xiàng)目越大,開發(fā)速度就越慢。

[root@yang-218 yangyun]# tree . ├── asset #資產(chǎn)功能目錄 │ ├── __init__.py │ ├── models.py #資產(chǎn)數(shù)據(jù)庫(kù)結(jié)構(gòu)文件 │ └── views.py #資產(chǎn)視圖文件 ├── user #用戶功能目錄 │ ├──__init__.py │ ├── models.py #用戶數(shù)據(jù)庫(kù)結(jié)構(gòu)文件 │ └── views.py #用戶視圖配置文件 ├── config.py #公共配置文件 ├── requirements.txt #需要的安裝包 ├── run.py #主運(yùn)行文件 ├── static #靜態(tài)文件目錄,css,js, p_w_picpath等 └── templates #靜態(tài)頁面存放目錄 ├── asset #asset功能模塊頁面存放目錄 │ └── index.html ├── index.html #首頁 └── user └── index.html
[root@yang-218 yangyun]# cat run.py
from flask import Flask from asset import asset from user import user apple=Flask(__name__, template_folder='templates', #指定模板路徑,可以是相對(duì)路徑,也可以是絕對(duì)路徑。 static_folder='static', #指定靜態(tài)文件前綴,默認(rèn)靜態(tài)文件路徑同前綴 #static_url_path='/opt/auras/static', #指定靜態(tài)文件存放路徑。 ) apple.register_blueprint(asset,url_prefix='/asset') #注冊(cè)asset藍(lán)圖,并指定前綴。 apple.register_blueprint(user) #注冊(cè)u(píng)ser藍(lán)圖,沒有指定前綴。 if __name__=='__main__': apple.run(host='0.0.0.0',port=8000,debug=True) #運(yùn)行flask http程序,host指定監(jiān)聽I(yíng)P,port指定監(jiān)聽端口,調(diào)試時(shí)需要開啟debug模式。
其它的功能模塊配置相似
1) __init__.py文件配置
[root@yang-218 asset]# cat __init__.py
from flask import Blueprint
asset=Blueprint('asset',
__name__,
#template_folder='/opt/auras/templates/', #指定模板路徑
#static_folder='/opt/auras/flask_bootstrap/static/',#指定靜態(tài)文件路徑
)
import views2) views.py文件配置
[root@yang-218 asset]# cat views.py
from flask import render_template
from asset import asset
@asset.route('/') #指定路由為/,因?yàn)閞un.py中指定了前綴,瀏覽器訪問時(shí),路徑為http://IP/asset/
def index():
print'__name__',__name__
returnrender_template('asset/index.html') #返回index.html模板,路徑默認(rèn)在templates下3)前端頁面配置
[root@yang-218 yangyun]# echo 'This isasset index page...' >templates/asset/index.html
此處配置和上述asset的配置一致
1) __init__.py配置
[root@yang-218 yangyun]# cat user/__init__.py
from flask import Blueprint
user=Blueprint('user',
__name__,
)
import views2) views.py配置
[root@yang-218 yangyun]# cat user/views.py
from flask import render_template
from user import user
@user.route('/')
def index():
print'__name__',__name__
returnrender_template('user/index.html')3) 靜態(tài)頁面配置
echo 'This is User page....' >templates/user/index.html
主要作用是記錄需要的依賴包,新環(huán)境部署時(shí)安裝如下依賴包即可,pip安裝命令: pip install -r requirements.txt
[root@yang-218 yangyun]# catrequirements.txt Flask==0.10.1 Flask-Bootstrap==3.3.5.6 Flask-Login==0.2.11 Flask-SQLAlchemy==2.0 Flask-WTF==0.12
后端運(yùn)行程序
[root@yang-218 yangyun]# python run.py *Running on http://0.0.0.0:8000/ (Press CTRL+C to quit) *Restarting with stat
前端訪問asset頁面

前端訪問user頁面

為什么出現(xiàn)404?因?yàn)樵趓un.py里沒有指定前綴,所以u(píng)rl里不需要加user。

以上
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享文章:Flaskblueprint藍(lán)圖按功能模塊化架構(gòu)實(shí)例-創(chuàng)新互聯(lián)
標(biāo)題URL:http://www.chinadenli.net/article6/dgseig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、網(wǎng)站收錄、云服務(wù)器、響應(yīng)式網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容