1.初始的项目目录
-apps 包 ------存放app
-user文件夹 -------就是一个app
-models.py --------存放表模型
-views.py -------存放主代码
-ext包
-init.py -------实例化db对象
-manage.py -----运行项目的入口
-setting.py -----配置文件
2.各文件内容
manage.py
from apps import count_tion
from flask_migrate import Migrate, MigrateCommand
from apps import db
from flask_script import Manager
app = 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 = True
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:123@127.0.0.1:3306/flask?charset=utf8'
SECRET_KEY = 'kdjklfjkd87384hjdhjh'
SQLALCHEMY_POOL_SIZE = 5
SQLALCHEMY_POOL_TIMEOUT = 30
SQLALCHEMY_POOL_RECYCLE = -1
ext下的__init__.py
from flask_sqlalchemy import SQLAlchemy
#创建一个映射对象 连接app和orm 映射
db = SQLAlchemy()
apps下的__init__.py
from flask import Flask
from apps.user.views import user_bp
import settings
from .user.models import User
from ext import db
def 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'