项目拆分
- 目的
- 解耦合
- 代码结构更清晰
from flask_migrate import MigrateCommand
from flask_script import Manager
from App import create_app
app = create_app()
manager = Manager(app=app)
# 添加命令行操作
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
初始化,因为模板文件默认在当前app目录下,所以要传入上级目录‘…/templates’,此处仿照Django使用绝对路径
加载配置文件中的develop(开发环境),在配置文件中有多套环境,方便更换。
App/init.py
from flask import Flask
from App.ext import init_ext
from App.settings import envs
from App.views import init_blue
def create_app():
app = Flask(__name__, template_folder=settings.TEMPLATE_FOLDER)
# 初始化配置
app.config.from_object(envs.get('develop'))
# 注册蓝图,初始化蓝图
init_blue(app)
# 初始化第三方插件,库
init_ext(app)
return app
App/settings.py
import os
# 绝对路径
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_FOLDER = os.path.join(BASE_DIR, 'templates')
def get_db_uri(dbinfo):
""" database setting """
ENGINE = dbinfo.get('ENGINE') or 'mysql'
DRIVER = dbinfo.get('DRIVER') or 'pymysql'
USER = dbinfo.get('USER') or 'root'
PASSWORD = dbinfo.get('PASSWORD') or 'root'
HOST = dbinfo.get('HOST') or 'localhost'
PORT = dbinfo.get('PORT') or '3306'
NAME = dbinfo.get('NAME') or 'test'
return "{}+{}://{}:{}@{}:{}/{}".format(ENGINE, DRIVER, USER, PASSWORD, HOST, PORT, NAME)
class Config:
DEBUG = False
TESTING = False
SECRET_KEY = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
SQLALCHEMY_TRACK_MODIFICATIONS = False
class DevelopConfig(Config):
""" 开发环境配置 """
DEBUG = True
DATABASE = {
"ENGINE": 'mysql',
'DRIVER': 'pymysql',
'USER': 'root',
'PASSWORD': 'rock1204',
'HOST': 'localhost',
'PORT': '3306',
'NAME': 'Python1804FlaskProject'
}
SQLALCHEMY_DATABASE_URI = get_db_uri(DATABASE)
class TestingConfig(Config):
""" 测试环境配置 """
TESTING = True
DATABASE = {
"ENGINE": 'mysql',
'DRIVER': 'pymysql',
'USER': 'root',
'PASSWORD': 'rock1204',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': 'Python1804FlaskProjectTesting'
}
SQLALCHEMY_DATABASE_URI = get_db_uri(DATABASE)
class StagingConfig(Config):
""" 演示环境配置 """
DATABASE = {
"ENGINE": 'mysql',
'DRIVER': 'pymysql',
'USER': 'root',
'PASSWORD': 'rock1204',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': 'Python1804FlaskProjectStaging'
}
SQLALCHEMY_DATABASE_URI = get_db_uri(DATABASE)
class ProductConfig(Config):
""" 上线环境配置 """
DATABASE = {
"ENGINE": 'mysql',
'DRIVER': 'pymysql',
'USER': 'root',
'PASSWORD': 'rock1204',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': 'Python1804FlaskProjectProduct'
}
SQLALCHEMY_DATABASE_URI = get_db_uri(DATABASE)
envs = {
'develop': DevelopConfig,
'testing': TestingConfig,
'staging': StagingConfig,
'product': ProductConfig
}
App/models.py
添加一个学生模型
from App.ext import db
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
s_name = db.Column(db.String(16))
App/views.py
在views中添加几个url用来对学生增删改查,详细见github代码
使用时先初始化数据库:python manage.py db init
这时会出现migrations文件夹。
如果想更新表执行:python manage.py db upgrade
如果想回退表执行:python manage.py db downgrade
修改models.py
class People(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
p_name = db.Column(db.String(16))
创建迁移文件
python manage.py db migrate --message '创建人模型'
调用upgrade继续更新表
python manage.py db upgrade
之后在数据库中可以看到新加的People模型
源码地址:https://github.com/565785929/StudyFlask/tree/master/d09