温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Flask blueprint蓝图按功能模块化架构实例

发布时间:2020-08-23 19:02:02 来源:网络 阅读:18976 作者:杨云1028 栏目:开发技术

使用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, p_w_picpath等
└── 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页面

Flask blueprint蓝图按功能模块化架构实例


前端访问user页面

Flask blueprint蓝图按功能模块化架构实例

为什么出现404?因为在run.py里没有指定前缀,所以url里不需要加user

Flask blueprint蓝图按功能模块化架构实例

 以上

 

 

 

 

 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI