0

我只是学习使用 Flask,但不明白为什么我的代码不起作用。这里是:

#运行py

import os

from app.create_app import create_app

config_name = os.getenv('FLASK_CONFIG')

app = create_app(config_name)

if __name__ == "__main__":
    app.run(debug=True)

#create_app.py

from config import app_config
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def create_app(config_name):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_object(app_config[config_name])
    app.config.from_pyfile('config.py')
    db.init_app(app)

    return app

#config.py

class Config(object):
    """
    Common configurations
    """

    # Put any configurations here that are common across all environments


class DevelopmentConfig(Config):
    """
    Development configurations
    """

    DEBUG = True
    SQLALCHEMY_ECHO = True


class ProductionConfig(Config):
    """
    Production configurations
    """

    DEBUG = False

app_config = {
    'development': DevelopmentConfig,
    'production': ProductionConfig
}

当我运行我的应用程序时,我会收到此错误:

Traceback (most recent call last):
  File "<input>", line 7, in <module>
  File "C:\Users\Dess\Desktop\iv\flask\app\create_app.py", line 27, in create_app
NameError: name 'app_config' is not defined

我在create_app中指出了app_config,但是run.py没有找到。我会很高兴你的帮助

4

1 回答 1

1

嘿,您应该在 #create.py 将 app_config 更改为 app.config。

于 2020-10-12T13:31:04.313 回答