2018-07-11

flask使用蓝图

1.首先创建蓝图对象


2.在app/__init__.py注册蓝本

3.蓝本定义的程序路由

flask连接mysql数据库和数据库迁移

1.在config.py的文件中配置数据库信息

2.在app/__init__.py完成app和数据的关联,并生成一个可以操作app数据库的SQLAlchemy实例db

3.创建models模块 models.py

4.进入shell 导入db 创建表

5.首先在虚拟环境中安装Flask-Migrate:

(venv) $ pip install flask-migrate

6.在manage.py配置Falsk-Migrate

7.在维护数据库迁移之前,要使用init 子命令创建迁移仓库:

(venv) $ python hello.py db init

8.migrate 子命令用来自动创建迁移脚本:

(venv) $ python hello.py db migrate -m "initial migration"

INFO [alembic.migration] Context impl SQLiteImpl.

INFO [alembic.migration] Will assume non-transactional DDL.

INFO [alembic.autogenerate] Detected added table 'roles'

INFO [alembic.autogenerate] Detected added table 'users'

INFO [alembic.autogenerate.compare] Detected added index

'ix_users_username' on '['username']'

Generating /home/flask/flasky/migrations/versions/1bc

594146bb5_initial_migration.py...done

9.更新数据库

(venv) $ python hello.py db upgrade

INFO [alembic.migration] Context impl SQLiteImpl.

INFO [alembic.migration] Will assume non-transactional DDL.

INFO [alembic.migration] Running upgrade None -> 1bc594146bb5, initial migration

你可能感兴趣的:(2018-07-11)