使用pycharm创建一个flask项目,默认的项目结构:
app01  // 项目名称
—— static
—— templates
—— venv   // 虚拟环境
—— app.py
然后我们进行以下拆分,仅供参考:
app01  // 项目名称
—— doc  // 文档文件夹
————documents.md
—— app  // 应用文件夹
———— __init__.py // 初始化app
———— models.py // 模板文件
———— views.py // 视图函数
———— ext.py  // 第三方扩展
———— settings.py // 配置扩展
———— static
———— templates
—— venv   // 虚拟环境
—— manage.py
app/__init__.py
from flask import Flask

from app import settings
from app.ext import init_ext
from app.views import init_first_blue


def create_app():
    app = Flask(__name__)
    # 配置加载,开发环境
    app.config.from_object(settings.config.get('development'))
    # 初始化蓝图
    init_first_blue(app)

    # 初始化扩展
    init_ext(app)

    return app
app/ext.py
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)
app/settings.py
import os

basedir = os.path.abspath(os.path.dirname(__file__))


class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    @staticmethod
    def init_app(app):
        pass


def get_db_uri(database):
    ENGINE = database.get('ENGINE') or 'mysql'
    DRIVER = database.get('DRIVER') or 'pymysql'
    USER = database.get('USER') or 'root'
    PASSWORD = database.get('PASSWORD') or ''
    HOST = database.get('HOST') or 'localhost'
    PORT = database.get('PORT') or '3306'
    DB = database.get('DB') or 'spider'
    return "{}+{}://{}:{}@{}:{}/{}".format(ENGINE, DRIVER, USER, PASSWORD, HOST, PORT, DB)


class DevelopmentConfig(Config):
    DEBUG = True
    DATABASE ={
        'ENGINE': 'mysql',
        'DRIVER': 'pymysql',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '3306',
        'DB': 'test'
    }
    # # 使用sqlite
    # SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
    #                           'sqlite:///' + os.path.join(basedir, 'data-dev.sqlite')
    # 使用mysql
    SQLALCHEMY_DATABASE_URI = get_db_uri(DATABASE)


class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or \
                              'sqlite://'


class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
                              'sqlite:///' + os.path.join(basedir, 'data.sqlite')


config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'default': DevelopmentConfig
}
app/views.py
import random

from flask import Blueprint
# 创建蓝图
from app.ext import db
from app.models import Student

blue = Blueprint('first_blue', __name__)


# 初始化蓝图
def init_first_blue(app):
    app.register_blueprint(blueprint=blue)


@blue.route('/')
def index():
    return 'Hello World!'


@blue.route('/student')
def students():
    student = Student()
    student.name = '老王-%s' % random.randrange(100)
    db.session.add(student)
    db.session.commit()
    return 'Students Create Success!'
app/models.py
from app.ext import db


class Student(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(12))
manage.py
from flask_migrate import MigrateCommand
from flask_script import Manager

from app import create_app

app = create_app()

manager = Manager(app)
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()

发表评论

后才能评论