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__.pyfrom 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.pyfrom 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.pyimport 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.pyimport 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.pyfrom 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.pyfrom 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()
声明:1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!