1 在blue文件夹下创建项目app
与项目app同级的文件一般有项目启动文件(文件名随意,这里是manate.py),项目的配置文件(文件名也随意,这里叫config.py) 此外跟app同级的还应该有数据库迁移的文件夹和测试文件夹
--------------项目配置文件---------------
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config (object) :
SECRET_KEY = os.urandom(24 )
@staticmethod
def init_app (app) :
pass
class DevelopmentConfig (Config) :
DEBUG = True
class TestingConfig (Config) :
DEBUG = False
config = {
'development' : DevelopmentConfig,
'test' : TestingConfig
}
-------------项目启动文件-----------------
from app import crate_app
app = crate_app('development' )
if __name__ == '__main__' :
app.run()
2 app下的文件和目录
static 用于存放静态文件,如js,css,img等 templates 用于保存模板 init文件 可以在其中写静态工厂方法,用于创建项目实例 注册蓝本 models及其他扩展模块 app下的子项目
--------------app的__init__.py----------
from flask import Flask,render_template
from config import config
def crate_app (config_name) :
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
from .one import xxoo as blue_one
app.register_blueprint(blue_one)
return app
3 app的子项目
一个app下会有多个功能模块,为了方便管理可以把他们分开 每个子项目下有自己的视图函数模块(可以有一个或者多个视图函数模块),错误处理模块及其他可能会用到的模块 init模块,用于创建蓝本,配置路由
----one的__init__.py---------
创建一个名为mark的蓝本,赋值给xxoo
-----------------------------
from flask import Blueprint
xxoo = Blueprint('mark' , __name__)
from . import views,goodview
----------------one中的视图函数---------------------
from . import xxoo
@xxoo.route("/yes")
def hello_blueprint () :
return "hello blueprint!"
@xxoo.route('/ofo')
def testURLFOR () :
return "test url_for"
--------需要注意的是:-------------------
用了蓝本之后url_for('mark.hello_bluprint' )
参数中的视图函数名前面要加上蓝本名
-------