1.初始的项目目录
-apps 包 ------存放app
-user文件夹 -------就是一个app
-models.py --------存放表模型
-views.py -------存放主代码
-ext包
-init.py -------实例化db对象
-manage.py -----运行项目的入口
-setting.py -----配置文件
2.各文件内容
manage.py
from apps import count_tionfrom flask_migrate import Migrate, MigrateCommand
from apps import db
from flask_script import Managerapp = count_tion()
# print(app)
manager = Manager(app)
Migrate(app, db)
manager.add_command("db", MigrateCommand)if __name__ == '__main__':# print(app.debug)manager.run()
setting.py
class Config:# ENV = 'development'DEBUG = TrueSQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:123@127.0.0.1:3306/flask?charset=utf8'SECRET_KEY = 'kdjklfjkd87384hjdhjh'SQLALCHEMY_POOL_SIZE = 5SQLALCHEMY_POOL_TIMEOUT = 30SQLALCHEMY_POOL_RECYCLE = -1
ext下的__init__.py
from flask_sqlalchemy import SQLAlchemy#创建一个映射对象 连接app和orm 映射
db = SQLAlchemy()
apps下的__init__.py
from flask import Flaskfrom apps.user.views import user_bp
import settings
from .user.models import User
from ext import dbdef count_tion():app = Flask(__name__) # 实例化app对象app.config.from_object(settings.Config) # 加载配置db.init_app(app) #db对象与app绑定# 注册蓝图app.register_blueprint(user_bp)return app
apps下user下的views.py
from flask import Blueprint
user_bp = Blueprint('user',__name__) #创建蓝图对象@user_bp.route('/',methods=['GET'])
def register():return 'register'