我是python和flask框架的新手,这看似微不足道,但我陷入了僵局。当我尝试运行Flask应用程序时,出现错误 keyError:'default'。
这是我的代码,分别放在三个不同的 .py 模块
manage.py
import os
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand
from app.models import User, Role
from app import create_app, db
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
@app.shell_context_processor
def make_shell_context():
return dict(db=db, User=User, Role=Role)
manager.add_command('shell', Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config:
SECRET_KEY = 'SECRET_KEY' or 'hard to guess string'
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]'
FLASKY_MAIL_SENDER = 'Flasky Admin <flasky@example.com>'
FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')
MAIL_SERVER = 'smtp.googlemail.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
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 ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'sqlite:///' + os.path.join(basedir, 'data.sqlite')
config = {
'development': DevelopmentConfig,
'production': ProductionConfig,
'default': DevelopmentConfig
}
app / __ init __。py
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_mail import Mail, Message
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from config import config
bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
app.config[config_name].init_app(app)
bootstrap.init_app(app)
mail.init_app(app)
moment.init_app(app)
db.init_app(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
这是正在获取的追溯信息
Traceback (most recent call last):
File "/home/charlson/Vscode/flasky/venv/bin/flask", line 11, in <module>
sys.exit(main())
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/flask/cli.py", line 513, in main
cli.main(args=args, prog_name=name)
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/flask/cli.py", line 380, in main
return AppGroup.main(self, *args, **kwargs)
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/flask/cli.py", line 423, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/flask/cli.py", line 152, in __init__
self._load_unlocked()
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/flask/cli.py", line 176, in _load_unlocked
self._app = rv = self.loader()
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/flask/cli.py", line 237, in load_app
rv = locate_app(self.app_import_path)
File "/home/charlson/Vscode/flasky/venv/lib/python3.6/site-packages/flask/cli.py", line 90, in locate_app
__import__(module)
File "/home/charlson/Vscode/flasky/flasky.py", line 8, in <module>
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
File "/home/charlson/Vscode/flasky/app/__init__.py", line 19, in create_app
app.config[config_name].init_app(app)
KeyError: 'default'
期望在本地计算机上的端口http://localhost:5000上有一个简单的网页
任何帮助都会令人感激
答案 0 :(得分:0)
在加载配置即app.config[config_name].init_app(app)
后删除代码app.config.from_object(config[config_name])
您已成功将conf数据加载到应用程序,然后可以访问在config类中指定的配置值。就像访问普通字典一样,因此它没有像default这样的任何属性会导致错误。
在删除代码app.config[config_name].init_app(app)
后尝试运行应用程序,它将正常工作。
请参阅此page以获取更多信息