环境:wsl+flask
最近一直在跟着flask web的书学习flask框架,但是最近遇到了两个问题
- sqlalchemy的连接问题
- sqlite no such table的问题
不知道以上两个问题是不是有联系的
首先是第一个问题,已经在config.py中设置了诸如sqlalchemy_database_uri和sqlalchemy_track_modification的属性,代码如下:
config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config:
SECRET_KEY = os.environ.get("SECRET_KEY") or 'hard to guess string'
MAIL_SERVER = os.environ.get("MAIL_SERVER","smtp.googlemail.com")
MAIL_PORT = int(os.environ.get("MAIL_PORT",'587'))
MAIL_USE_TLS = os.environ.get("MAIL_USE_TLS",'true').lower() in ['true','on','1']
MAIL_USERNAME = os.environ.get("MAIL_USERNAME")
MAIL_PASSWORD = os.environ.get("MAIL_PASSWORD")
FLAKSY_MAIL_SUBJECT_PREFIX = '[Flasky]'
FLASKY_MAIL_SENDER = 'Flasky Admin <flasky@example.com>'
FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')
SQLALCHEMY_TRACK_MODIFICATIONS = False
@staticmethod
def init_app(app):
pass
class DevelopmentConfig(Config):
DEBUG =True
SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or 'sqlite:///' +os.path.join(basedir,'data-dev.sqlite')
class TestingConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or 'sqlite:///'+os.path.join(basedir,'data-test.sqlite')
class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir,'data.sqlite')
config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'production': ProductionConfig,
'default': DevelopmentConfig
}
def __main__():
path = os.environ.get('DEV_DATABASE_URL') or 'sqlite:///'+os.path.join(basedir,'data-dev.sqlite')
print(path)
print(type(path))
if __name__ == "__main__":
__main__()
使用的过程如下:首先是app/__init__.py
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.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')
return app
然后在flasky.py中创建应用,flasky.py
import os
from app import create_app,db
from app.models import User,Role
from flask_migrate import Migrate
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
# app = create_app('default')
migrate = Migrate(app,db)
@app.shell_context_processor
def make_shell_context():
return dict(db=db,User=User,Role=Role)
@app.cli.command()
def test():
"""run the unit tests"""
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
但是在进入flask shell后还是会出现报错,如下:
UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
但是flask shell 中
输入db并回车又能够返回正常的
'<SQLAlchemy engine=sqlite:////mnt/d/flasky/data-dev.sqlite>'
目前网上还没找到和我有相同问题的情况