Flask blueprint藍圖按功能模塊化架構實例

使用flask作爲開發框架,一定要按功能模塊化,否則到了後面項目越大,開發速度就越慢。

1Flask模塊化結構規劃

[root@yang-218 yangyun]# tree
.
├── asset               #資產功能目錄
│   ├── __init__.py
│   ├── models.py        #資產數據庫結構文件
│   └── views.py         #資產視圖文件
├── user                #用戶功能目錄
│  ├──__init__.py
│  ├── models.py          #用戶數據庫結構文件
│  └── views.py           #用戶視圖配置文件
├── config.py             #公共配置文件
├── requirements.txt        #需要的安裝包
├── run.py               #主運行文件
├── static               #靜態文件目錄,css,js, image等
└── templates             #靜態頁面存放目錄
   ├── asset           #asset功能模塊頁面存放目錄
    │  └── index.html
    ├── index.html         #首頁
    └── user                        
        └── index.html

2run.py主運行文件配置

[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', #指定模板路徑,可以是相對路徑,也可以是絕對路徑。 
        static_folder='static',  #指定靜態文件前綴,默認靜態文件路徑同前綴
        #static_url_path='/opt/auras/static',     #指定靜態文件存放路徑。
         )
apple.register_blueprint(asset,url_prefix='/asset')    #註冊asset藍圖,並指定前綴。
apple.register_blueprint(user)      #註冊user藍圖,沒有指定前綴。
 
if __name__=='__main__':
         apple.run(host='0.0.0.0',port=8000,debug=True)  #運行flask http程序,host指定監聽IP,port指定監聽端口,調試時需要開啓debug模式。


3asset功能模塊配置

其它的功能模塊配置相似

 

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/',#指定靜態文件路徑
                   )
 
import views

 

2) views.py文件配置

[root@yang-218 asset]# cat views.py

from flask import  render_template
from asset import asset
 
@asset.route('/')              #指定路由爲/,因爲run.py中指定了前綴,瀏覽器訪問時,路徑爲http://IP/asset/
def index():                         
         print'__name__',__name__
         returnrender_template('asset/index.html')  #返回index.html模板,路徑默認在templates下

 

3)前端頁面配置

[root@yang-218 yangyun]# echo 'This isasset index page...' >templates/asset/index.html

 

4user功能模塊配置

此處配置和上述asset的配置一致

1) __init__.py配置

[root@yang-218 yangyun]# cat  user/__init__.py

from flask import Blueprint
user=Blueprint('user',
                   __name__,
                   )
import views

2) 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) 靜態頁面配置

echo 'This is User page....'  >templates/user/index.html

 

5requirements.txt文件配置

主要作用是記錄需要的依賴包,新環境部署時安裝如下依賴包即可,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

 

6、瀏覽器訪問測試

後端運行程序

[root@yang-218 yangyun]# python run.py
 *Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
 *Restarting with stat

前端訪問asset頁面

wKioL1X6FUTg52DlAABF2vmZqMA453.jpg


前端訪問user頁面

wKiom1X6EwyDfQ7XAABu-gqfM60350.jpg

爲什麼出現404?因爲在run.py裏沒有指定前綴,所以url裏不需要加user

wKioL1X6FUSyI3DjAAA7JI0SKS0527.jpg

 以上

 

 

 

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章