正在做的论坛的主程序

2016-09-30  本文已影响31人  michaeljacc

改了数据库重建表示是一件很头痛的事情,不用重建数据库,建一个新表-->迁移旧表->删除旧表
pip install flask flask-sqlalchemy flask-migrate flask-script

cls 清除
python autoMigarate.py db init
python autoMigarate.py db migarate
python autoMigarate.py db updata
cd web19

from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager

from models import db
# 这里 import 具体的 Model 类是为了给 migrate 用
# 如果不 import 那么无法迁移
# 这是 SQLAlchemy 的机制
from models.todo import Todo
from models.user import User
from models.node import Node
from models.topic import Topic

from routes.todo import main as routes_todo
from routes.node import main as routes_node
from routes.topic import main as routes_topic


app = Flask(__name__)
db_path = 'todo.sqlite'
manager = Manager(app)


def register_routes(app):
    app.register_blueprint(routes_todo, url_prefix='/todo')
    app.register_blueprint(routes_node, url_prefix='/node')
    app.register_blueprint(routes_topic, url_prefix='/topic')


def configure_app():
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
    app.secret_key = 'secret key'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///{}'.format(db_path)
    db.init_app(app)
    register_routes(app)


def configured_app():
    configure_app()
    return app


# 自定义的命令行命令用来运行服务器
@manager.command
def server():
    print('server run')
    # app = configured_app()
    config = dict(
        debug=True,
        host='0.0.0.0',
        port=3000,
    )
    app.run(**config)


def configure_manager():
    """
    这个函数用来配置命令行选项
    """
    Migrate(app, db)
    manager.add_command('db', MigrateCommand)


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

# gunicorn -b '0.0.0.0:80' redischat:app


上一篇下一篇

猜你喜欢

热点阅读