1. 拆分原理
2. 从manager.py入口开始
from App import createApp
app = createApp()
manager = Manager(app)
manager.add_command('db',MigrateCommand)
if __name__="__main__"
manager.run()
3. 进入到inti.py中
from flask import Flask
from App.views import init_views
from App.ext import init_ext
def createApp(env=None):
app = Flask(__name__)
init_views(app) # 导入views,主要是蓝图
init_ext(app) # 导入ext扩展
app.config.from_object(config.get(env or 'default'))
return app
4. settings.py配置
class BaseConfig():
DEBUG = True
SECRET_KEY = 'FKADJFIfjkdfjsjf93w93r8@JJ4'
SESSION_TYPE = 'redis'
TESTING = False
class DevelopConfig(BaseConfig):
SQLALCHEMY_DATABASE_URI = 'sqlite:////dev.db'
class TestConfig(BaseConfig):
TESTING = True
SQLALCHEMY_DATABASE_URI = 'sqlite:////test.db'
class ProductConfig(BaseConfig):
DEBUG = False
SQLALCHEMY_DATABASE_URI = 'sqlite:////pro.db'
config = {
'dev':DevelopConfig,
'test':TestConfig,
'pro':ProductConfig
}
5. ext.py扩展,用来初始化插件
db = SQLAlchemy()
migrate = Migrate()
def init_ext(app):
db.init_app(app)
migrate.init_app(db,app)
6. views.py中配置
from flask_blueprint import Blueprint
from App.models import *
blue = Blueprint('blue',__name__)
def inti_views(app):
app.register_blueprint(blueprint=blue)
7. models.py中的配置
from App.ext import db
class Student(db.Model):
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
网友评论