在Flask项目中建立一个models.py中配置数据库如下:
1 # -*- coding: utf-8 -*- 2 import os 3 from flask import Flask 4 from itsdangerous import TimedJSONWebSignatureSerializer as Serializer 5 from flask import current_app 6 from .import db 7 from flask_sqlalchemy import SQLAlchemy 8 from werkzeug.security import generate_password_hash, check_password_hash 9 from flask_login import UserMixin 10 from flask_login import login_required 11 from flask_moment import Moment 12 from flask_bootstrap import Bootstrap 13 from .import login_manager 14 15 basedir = os.path.abspath(os.path.dirname(__file__)) 16 17 app = Flask(__name__) 18 #配置数据 19 app.config[‘SECRET_KEY‘] = ‘stong key‘ 20 app.config[‘SQLALCHEMY_DATABASE_URI‘] = ‘mysql://root:[email protected]:3306/flask‘ 21 app.config[‘SQLALCHEMY_COMMIT_TEARDOWN‘] = True 22 bootstrap = Bootstrap(app) 23 moment = Moment(app) 24 db = SQLAlchemy(app) 25 26 app.run(debug=True)