when you follow the book FlaskWeb developer to Chapter 7 to adjust the project structure, there is a UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set warning using flask_sqlalchemy
this is the config.py code
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
class Config:
app.config["SECRET_KEY"] = "hard to guess string"
app.config["MAIL_DEFAULT_SENDER"] = "123@qq.com" -sharp
app.config["SECRET_KEY"] = "hard to guess string"
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
def init_app(app):
pass
class DevelopmentConfig(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
class TestingConfig(Config):
TESTING = True
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:430202@127.0.0.1:3306/test1"
class ProductionConfig(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
}
this is the app__init__.py code
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from config import config
from flask_login import LoginManager
bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()
login_manager=LoginManager()
login_manager.session_protection="strong"
login_manager.login_view="auth.login"
def create_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 .main import main as main_blueprint
app.register_blueprint(main_blueprint)
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint,url_prefix="/auth")
-sharp
return app
this is the test file
import unittest
from flask import current_app
from app import create_app, db
class BasicsTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app("testing")
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def test_app_exists(self):
self.assertFalse(current_app is None)
def test_app_is_testing(self):
self.assertTrue(current_app.config["TESTING"])
the error message when running python manage.py test is as follows:
br > UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. UsersDELLPycharmProjectsbookvenvlibsitelypackagesflaskchemistry Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:/:memory:".
"Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set."
test_app_exists (test_basic.BasicsTestCase). CJV UsersDELLPycharmProjectsbookvenvenvlibsitelypackagesflaskexamples sqlalchemicalchemicals initability.pyRom 774: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:/:memory:".
"Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set."
have tried all the solutions that have similar problems on the Internet, but still haven"t solved them. I really don"t know what the problem is. Can anyone take a look at it?