连接数据库,
配置信息
设置博客导航栏
导入 flask-sqlalchemy,flask-script,,flask-migrate模块
1:app.py代码
from flask import Flask,render_template
import config
app = Flask(__name__)
app.config.from_object(config)
@app.route('/')
def index():
return render_template('index.html')
# @app.route('/user/<name>')
# def user(name):
# return render_template('user.html', name=name)
if __name__ == '__main__':
app.run()
2:配置文件config.py
import os
DEBUG = True
SECRET_KEY = os.urandom(24)
DB_URI ='mysql+mysqldb://{}:{}@{}:{}/{}?charset=utf-8'.format(USERNAME,PASSWORD,HOSTNAME,PORT,DATABASE)
SQLALCHEMY_DATABASE_URL =DB_URI
3:exts.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
4:manage.py文件
from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from app import app
from exts import db
manager = Manager(app)
#使用Migrate 绑定app和db
migrate = Migrate(app,db)
#添加迁移脚本的命令到manager中
manager.add_command('db',MigrateCommand)
if __name__ == '__main__':
manager.run()
5:models.py
from exts import db