美文网首页
flask项目的拆分

flask项目的拆分

作者: 时光的味道_c179 | 来源:发表于2018-07-11 20:06 被阅读0次

    flask的拆分:
    第二阶段的拆分包括以下:
    init.py:用于整个项目的初始化。
    manage.py:负责项目的运行。
    settings.py:整个项目的配置。
    ext.py:插件的配置。
    models.py:生成模型。
    views.py:书写视图函数。

    2018-07-11 20-04-57 的屏幕截图.png
    2018-07-11 20-04-51 的屏幕截图.png
    2018-07-11 20-04-47 的屏幕截图.png
    2018-07-11 20-04-44 的屏幕截图.png
    2018-07-11 20-04-41 的屏幕截图.png
    第一种拆分:
    init.py中包括:
    from App.views import blue
    def create_app():
    app = Flask(name)
    app.register_blueprint(blueprint = blue)
    return app

    manage.py文件中:
    from flask import Flask
    app = create_app()
    manage = Manage(app)
    if name = main:
    manage.py

    views.py文件中:
    blue = Blueprint('blue',name)
    @blue.route('/index/')
    def index():
    return 'hello world!'

    第二种拆分:
    init.py:
    from flask import Flask
    from App.ext import init_ext
    from App.settings import config
    from App.views import blue, init_blue

    def create_app(env_name = None):
    app = Flask(name)

    #配置
    app.config.from_object(config.get(env_name or 'default'))
    
    #初始化插件
    init_ext(app)
    #初始化蓝图
    init_blue(app)
    return 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=app, db=db)

    models.py:
    from App.ext import db

    class User(db.Model):
    id=db.Column(db.Integer,autoincrement=True,primary_key=True)
    name = db.Column(db.String(30))
    age = db.Column(db.Integer)
    score = db.Column(db.Integer)

    views.py:
    from flask import Blueprint

    blue = Blueprint('blue',name)
    def init_blue(app):
    app.register_blueprint(blueprint=blue)
    @blue.route('/')
    def hello_world():
    return 'Hello World!'

    manage.py

    配置基类

    class BaseConfig():
    DEBUG = False
    TESTING = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SECRET_KEY = '&*(&*&%&$%^rfhtgfrte$swe#w#%%$'

    拼接操作

    def get_database_uri(DATABASE):
    db = DATABASE.get('DB') or 'mysql'
    driver = DATABASE.get('DRIVER') or 'pymysql'
    username = DATABASE.get('USERNAME') or 'root'
    password = DATABASE.get('PASSWORD') or 'root'
    host = DATABASE.get('HOST') or '127.0.0.1'
    port = DATABASE.get('PORT') or '3306'
    dbname =DATABASE.get('DBNAME') or 'FLASK01'

    return '{}+{}://{}:{}@{}:{}/{}'.format(db,driver,username,password,host,port,dbname)
    

    class DevelopmentConfig(BaseConfig):
    DEBUG = True

    DATABASE = {
        'DB':'mysql',
        'DRIVER':'pymysql',
        'USERNAME':'root',
        'PASSWORD':'root',
        'HOST':'127.0.0.1',
        'POST':'3306',
        'DBNAME':'flask01',
    }
    
    #数据库配置
    SQLALCHEMY_DATABASE_URI = get_database_uri(DATABASE)
    

    测试环境

    class TestingConfig(BaseConfig):
    DEBUG = True

    DATABASE = {
        'DB':'mysql',
        'DRIVER':'pymysql',
        'USERNAME':'root',
        'PASSWORD':'root',
        'HOST':'127.0.0.1',
        'POST':'3306',
        'DBNAME':'flask01',
    }
    
    #数据库配置
    SQLALCHEMY_DATABASE_URI = get_database_uri(DATABASE)
    

    演示环境

    class StagingConfig(BaseConfig):
    DEBUG = True

    DATABASE = {
        'DB':'mysql',
        'DRIVER':'pymysql',
        'USERNAME':'root',
        'PASSWORD':'root',
        'HOST':'127.0.0.1',
        'POST':'3306',
        'DBNAME':'flask01',
    }
    
    #数据库配置
    SQLALCHEMY_DATABASE_URI = get_database_uri(DATABASE)
    

    线上环境

    class ProductConfig(BaseConfig):
    DEBUG = True

    DATABASE = {
        'DB':'mysql',
        'DRIVER':'pymysql',
        'USERNAME':'root',
        'PASSWORD':'root',
        'HOST':'127.0.0.1',
        'POST':'3306',
        'DBNAME':'flask01',
    }
    
    #数据库配置
    SQLALCHEMY_DATABASE_URI = get_database_uri(DATABASE)
    

    config = {
    'develop':DevelopmentConfig,
    'testing':TestingConfig,
    'staging':StagingConfig,
    'product':ProductConfig,
    'default':DevelopmentConfig,
    }
    manage.py:
    from flask_script import Manager

    from App import create_app

    app = create_app()
    manage = Manager(app)
    if name == 'main':
    app.run()

    相关文章

      网友评论

          本文标题:flask项目的拆分

          本文链接:https://www.haomeiwen.com/subject/nsrkpftx.html