def create_app(): app = Flask(__name__) app.config.from_object('app.config') register_blueprint(app) db.init_app(app) mail.init_app(app) login_manager.init_app(app) login_manager.login_view = 'web.login' login_manager.login_message = '请先登录或注册' # 第一次运行成功之后可以把34-45行注释掉 with app.app_context(): db.create_all(app=app) if not Admin.query.filter_by(nickname='admin').first(): ad = Admin() ad.nickname = 'admin' ad.role = 'super' ad.password = generate_password_hash('123456') with db.auto_commit(): db.session.add(ad) return app
# -*- coding: utf-8 -*- """ @description: @copyright: (c) 2019/3/28 18:17 by Henry. """ from datetime import datetime __author__ = 'Henry' from app import create_app from app.models.base import db from app.models.admin import Admin app = create_app() with app.app_context(): with db.auto_commit(): # 创建一个超级管理员 user = Admin() user.nickname = 'Admin' user.username = '******' user.password = '******' user.email = '*****@*****.**' db.session.add(user)