python_flask分離開発

2299 ワード

ディレクトリ構造:
flasks                                                           # 
        |--------apps                                            # 
                   |--------admin.py                             #   
        |--------static                                          # 
        |--------templates                                       # 
        |--------views                                           # 
                    |--------admin.py                            # 
                    |--------manage.py                           # 
        |--------upd.py                                          # 

内容説明:
#apps/admin.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import pymysql

db = SQLAlchemy()

#####################################################
########           
class User(db.Model):
    __tablename__ = 'user'
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(32))
    pas=db.Column(db.String(32))
    passs=db.Column(db.String(32))

########           
#####################################################

def create_app():
    app=Flask(__name__)
    # 
SDU='mysql+pymysql://root:[email protected]:3306/ccc'                                        
app.config['SQLALCHEMY_DATABASE_URI']=SDU
    return app
#views/admin.py
from flask import Blueprint
from apps.admin import *

admin=Blueprint('admin',__name__)

@admin.route('/')
def index():
    return 'admin_index'

@admin.route('/show')
def show():
    return 'admin_show'
manafe.py
from apps.admin import create_app
from flask_migrate import Migrate,MigrateCommand
from flask_script import Manager
from views.admin import *
app=create_app()
app.register_blueprint(admin,url_perfil='/')

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

if __name__ == '__main__':
    manager.run()
#upd.py
from manage import manager

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

コンソール入力python upd.py db init migrateの初期化に必要な環境python upd.py db migrateデータ移行スクリプトpython updを生成します.py db upgradeデータベース・テーブルの構造の更新