使用flask做为开发框架,必定要按功能模块化,不然到了后面项目越大,开发速度就越慢。css
[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
[root@yang-218 yangyun]# cat run.pyhtml
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模式。
其它的功能模块配置类似前端
1) __init__.py文件配置python
[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文件配置flask
[root@yang-218 asset]# cat views.pybootstrap
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
此处配置和上述asset的配置一致浏览器
1) __init__.py配置sass
[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
主要做用是记录须要的依赖包,新环境部署时安装以下依赖包便可,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
后端运行程序
[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页面
为何出现404?由于在run.py里没有指定前缀,因此url里不须要加user。
以上