为了账号安全,请及时绑定邮箱和手机立即绑定

mysql+flask _sqlalchemy报错

mysql+flask _sqlalchemy报错

跟着书本FlaskWeb开发学到第七章调整项目结构的时候,使用flask_sqlalchemy出现UserWarning:NeitherSQLALCHEMY_DATABASE_URInorSQLALCHEMY_BINDSisset的警告这是config.py代码fromflaskimportFlaskfromflask_sqlalchemyimportSQLAlchemyimportosapp=Flask(__name__)basedir=os.path.abspath(os.path.dirname(__file__))classConfig:app.config['SECRET_KEY']='hardtoguessstring'app.config['MAIL_DEFAULT_SENDER']='123@qq.com'#填邮箱,默认发送者app.config['SECRET_KEY']='hardtoguessstring'app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://root:430202@127.0.0.1:3306/test1'app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=Trueapp.config['SQLALCHEMY_COMMIT_ON_TEARDOWN']=Trueapp.config['FLASKY_ADMIN']='123@123.cn'@staticmethoddefinit_app(app):passclassDevelopmentConfig(Config):DEBUG=Trueapp.config['MAIL_SERVER']='smtp.qq.com'app.config['MAIL_PORT']=587app.config['MAIL_USE_TLS']=Trueapp.config['MAIL_USERNAME']='123@qq.com'app.config['MAIL_PASSWORD']='ddjogvrpawyebcdi'app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://root:430202@127.0.0.1:3306/test1'app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=Trueapp.config['SQLALCHEMY_COMMIT_ON_TEARDOWN']=TrueclassTestingConfig(Config):TESTING=Trueapp.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://root:430202@127.0.0.1:3306/test1'classProductionConfig(Config):app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://root:430202@127.0.0.1:3306/test1'config={'development':DevelopmentConfig,'testing':TestingConfig,'production':ProductionConfig,'default':DevelopmentConfig}这是app__init__.py代码fromflaskimportFlask,render_templatefromflask_bootstrapimportBootstrapfromflask_mailimportMailfromflask_momentimportMomentfromflask_sqlalchemyimportSQLAlchemyfromconfigimportconfigfromflask_loginimportLoginManagerbootstrap=Bootstrap()mail=Mail()moment=Moment()db=SQLAlchemy()login_manager=LoginManager()login_manager.session_protection='strong'login_manager.login_view='auth.login'defcreate_app(config_name):app=Flask(__name__)app.config.from_object(config[config_name])config[config_name].init_app(app)bootstrap.init_app(app)mail.init_app(app)moment.init_app(app)db.init_app(app)login_manager.init_app(app)from.mainimportmainasmain_blueprintapp.register_blueprint(main_blueprint)from.authimportauthasauth_blueprintapp.register_blueprint(auth_blueprint,url_prefix='/auth')#附加路由和自定义的错误页面returnapp这是test文件importunittestfromflaskimportcurrent_appfromappimportcreate_app,dbclassBasicsTestCase(unittest.TestCase):defsetUp(self):self.app=create_app('testing')self.app_context=self.app.app_context()self.app_context.push()db.create_all()deftearDown(self):db.session.remove()db.drop_all()self.app_context.pop()deftest_app_exists(self):self.assertFalse(current_appisNone)deftest_app_is_testing(self):self.assertTrue(current_app.config['TESTING'])运行pythonmanage.pytest的时候报错信息如下:C:UsersDELLPycharmProjectsbookvenvlibsite-packagesflask_sqlalchemy__init__.py:774:UserWarning:NeitherSQLALCHEMY_DATABASE_URInorSQLALCHEMY_BINDSisset.DefaultingSQLALCHEMY_DATABASE_URIto"sqlite:///:memory:".'NeitherSQLALCHEMY_DATABASE_URInorSQLALCHEMY_BINDSisset.'test_app_exists(test_basic.BasicsTestCase)...C:UsersDELLPycharmProjectsbookvenvlibsite-packagesflask_sqlalchemy__init__.py:774:UserWarning:NeitherSQLALCHEMY_DATABASE_URInorSQLALCHEMY_BINDSisset.DefaultingSQLALCHEMY_DATABASE_URIto"sqlite:///:memory:".'NeitherSQLALCHEMY_DATABASE_URInorSQLALCHEMY_BINDSisset.'目前把网上有类似问题的解决方案都尝试过了,还是没有解决,真的不知道问题出在哪,有人帮忙看一下吗?
查看完整描述

2 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

把Config类里的app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://root:430202@127.0.0.1:3306/test1'去掉。
                            
查看完整回答
反对 回复 2019-05-13
?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

我将congfig文件中的app.config['xxx']修改成['xxx']='xxx'的格式就不会出现这个提醒了,例如将app.config['SECRET_KEY']='hardtoguessstring'修改为SECRET_KEY='hardtoguessstring'不知道原因,但是确实这样操作后就没有警告了
                            
查看完整回答
反对 回复 2019-05-13
  • 2 回答
  • 0 关注
  • 968 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信