mysql+flask _sqlalchemy报错

跟着书本FlaskWeb开发学到第七章调整项目结构的时候,使用flask_sqlalchemy出现UserWarning:NeitherSQLALCHEMY_DATABASE_URInorSQLALCHEMY_BINDSisset的警告这是config.py代码
fromflaskimportFlask
fromflask_sqlalchemyimportSQLAlchemy
importos
app=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']=True
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN']=True
app.config['FLASKY_ADMIN']='123@123.cn'
@staticmethod
definit_app(app):
pass
classDevelopmentConfig(Config):
DEBUG=True
app.config['MAIL_SERVER']='smtp.qq.com'
app.config['MAIL_PORT']=587
app.config['MAIL_USE_TLS']=True
app.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']=True
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN']=True
classTestingConfig(Config):
TESTING=True
app.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_template
fromflask_bootstrapimportBootstrap
fromflask_mailimportMail
fromflask_momentimportMoment
fromflask_sqlalchemyimportSQLAlchemy
fromconfigimportconfig
fromflask_loginimportLoginManager
bootstrap=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_blueprint
app.register_blueprint(main_blueprint)
from.authimportauthasauth_blueprint
app.register_blueprint(auth_blueprint,url_prefix='/auth')
#附加路由和自定义的错误页面
returnapp
这是test文件
importunittest
fromflaskimportcurrent_app
fromappimportcreate_app,db
classBasicsTestCase(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.'目前把网上有类似问题的解决方案都尝试过了,还是没有解决,真的不知道问题出在哪,有人帮忙看一下吗?
慕尼黑的夜晚无繁华
浏览 969回答 2
2回答

喵喵时光机

把Config类里的app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://root:430202@127.0.0.1:3306/test1'去掉。

尚方宝剑之说

我将congfig文件中的app.config['xxx']修改成['xxx']='xxx'的格式就不会出现这个提醒了,例如将app.config['SECRET_KEY']='hardtoguessstring'修改为SECRET_KEY='hardtoguessstring'不知道原因,但是确实这样操作后就没有警告了
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript