1.初始的项目目录
-apps 包 ------存放app
-user文件夹 -------就是一个app
-models.py --------存放表模型
-views.py -------存放主代码
-ext包
-init.py -------实例化db对象
-manage.py -----运行项目的入口文章来源:https://www.toymoban.com/news/detail-639428.html
-setting.py -----配置文件文章来源地址https://www.toymoban.com/news/detail-639428.html
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'
到了这里,关于flask-----初始项目架构的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!