自动创建完成了templates、static两个资源文件夹,一个管理文件app.py
import os
from flask_migrate import MigrateCommand
from flask_script import Manager
# 根据当前系统环境检测运行环境
from APP import create_app
env = os.environ.get("FLASK_ENV","develop")
# 将当前的系统环境运用到项目中
app = create_app(env=env)
manager = Manager(app=app)
# 添加数据库更新指令
manager.add_command('db',MigrateCommand)
if __name__ == '__main__':
manager.run()
from flask import Flask
from APP.ext import init_ext
from APP.settings import envs
from APP.views import init_view
# 当前模块的初始化
def create_app(env):
app = Flask(__name__)
app.config.from_object(envs.get(env))
# 初始化依赖库
init_ext(app)
# 初始化蓝图
init_view(app)
return app
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
# 创建数据库操作对象
db = SQLAlchemy()
# 创建数据库变更
migrate = Migrate()
# 使用懒加载方式初始化
def init_ext(app):
db.init_app(app)
migrate.init_app(app,db)
from flask import Blueprint
# 创建蓝图
testBlue = Blueprint('testBlue',__name__)
# 注册蓝图
def init_view(app):
#注册蓝图
app.register_blueprint(testBlue)
# 蓝图使用
@testBlue.route('/')
def index():
return "index page..."
# 配置项目根目录
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# 格式化数据库连接信息
def get_db_uri(dbinfo):
engine = dbinfo.get("ENGINE") or "sqlite"
driver = dbinfo.get("DRIVER") or "sqlite"
user = dbinfo.get("USER") or ""
password = dbinfo.get("PASSWORD") or ""
host = dbinfo.get("HOST") or ""
prot = dbinfo.get("PROT") or ""
name = dbinfo.get("NAME") or ""
# 格式化数据库连接
# 数据库+驱动://用户名:密码@主机:端口/库名
return "{}+{}://{}:{}@{}:{}/{}".format(engine, driver, user, password, host, prot, name)
class Config:
DEBUG = True
TESTING = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
# 设置四个不同的环境
'''
开发环境
测试环境
演示环境
线上环境
'''
class DevelopConfig(Config):
DEBUG = True
# 数据库信息字典
dbinfo = {
"ENGINE":"mysql",
"DRIVER":"pymysql",
"USER":"root",
"PASSWORD":"123456",
"HOST":"localhost",
"PROT":"3306",
"NAME":"flask_day01",
}
SQLALCHEMY_DATABASE_URI = get_db_uri(dbinfo)
class TestConfig(Config):
TESTING = True
# 数据库信息字典
dbinfo = {
"ENGINE":"mysql",
"DRIVER":"pymysql",
"USER":"root",
"PASSWORD":"123456",
"HOST":"localhost",
"PROT":"3306",
"NAME":"flask_day01",
}
SQLALCHEMY_DATABASE_URI = get_db_uri(dbinfo)
class StagingConfig(Config):
# 数据库信息字典
dbinfo = {
"ENGINE":"mysql",
"DRIVER":"pymysql",
"USER":"root",
"PASSWORD":"123456",
"HOST":"localhost",
"PROT":"3306",
"NAME":"flask_day01",
}
SQLALCHEMY_DATABASE_URI = get_db_uri(dbinfo)
class ProductConfig(Config):
DEBUG = True
# 数据库信息字典
dbinfo = {
"ENGINE":"mysql",
"DRIVER":"pymysql",
"USER":"root",
"PASSWORD":"123456",
"HOST":"localhost",
"PROT":"3306",
"NAME":"flask_day01",
}
SQLALCHEMY_DATABASE_URI = get_db_uri(dbinfo)
envs = {
"develop":DevelopConfig,
"testing":TestConfig,
"staging":StagingConfig,
"product":ProductConfig,
"default":DevelopConfig,
}
from APP.ext import db
'''
其中写法和django大同小异,写完之后需要自定义一个save的方法,针对一个类有一个自己的save()方法
def save(self):
db.session.add(self)
db.session.commit()
'''
因为前边我把自己的管理文件app.py更名为了manage.py,所以在这儿我的 启动方式和 django一样
注意:上边使用分离的时候自定义了数据库更新的操作,其中命令为:
python manage.py(你的管理文件名称) db(自己定义) init —>初始化,因为不是内置命令,所有第一次使用的时候需要初始化
python manage.py db upgrade
启动命令
python manage.py runserver
python manage.py runserver -r -d (该方式是开启调试模式启动)
项目启动
学习课程来源于https://www.bilibili.com/video/BV1SJ411P7qb