美文网首页
项目的高级拆分

项目的高级拆分

作者: Challis | 来源:发表于2018-10-10 21:13 被阅读0次

    1. 拆分原理

    2. 从manager.py入口开始

    from App import createApp
    
    app = createApp()
    manager = Manager(app)
    manager.add_command('db',MigrateCommand)
    
    if __name__="__main__"
      manager.run()
    

    3. 进入到inti.py中

    from flask import Flask
    from App.views import init_views
    from App.ext import init_ext
    def createApp(env=None):
      app = Flask(__name__)
      
      init_views(app)    # 导入views,主要是蓝图
      init_ext(app)     # 导入ext扩展
      app.config.from_object(config.get(env or 'default'))
      return app
    

    4. settings.py配置

    class BaseConfig():
      DEBUG = True
      SECRET_KEY = 'FKADJFIfjkdfjsjf93w93r8@JJ4'
      SESSION_TYPE = 'redis'
      TESTING = False
    class DevelopConfig(BaseConfig):
      SQLALCHEMY_DATABASE_URI = 'sqlite:////dev.db'
    
    
    class TestConfig(BaseConfig):
      TESTING  = True
      SQLALCHEMY_DATABASE_URI = 'sqlite:////test.db'
    
      
    class ProductConfig(BaseConfig):
      DEBUG = False
      SQLALCHEMY_DATABASE_URI = 'sqlite:////pro.db'
    
    
    
    
    config = {
      'dev':DevelopConfig,
      'test':TestConfig,
      'pro':ProductConfig
    }
    

    5. ext.py扩展,用来初始化插件

    
    db = SQLAlchemy()
    migrate = Migrate()
    def init_ext(app):
      db.init_app(app)
      migrate.init_app(db,app)
    
    

    6. views.py中配置

    from flask_blueprint import Blueprint
    from App.models import *
    blue = Blueprint('blue',__name__)
    def inti_views(app):
      app.register_blueprint(blueprint=blue)
    

    7. models.py中的配置

    from App.ext import db
    class Student(db.Model):
      id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    
    

    相关文章

      网友评论

          本文标题:项目的高级拆分

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