Flask 配置文件 - 'DEBUG=True' 什么都不做 [英] Flask Config File - 'DEBUG=True' Do Nothing

查看:21
本文介绍了Flask 配置文件 - 'DEBUG=True' 什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为MyApp"的包中构建了一个大型烧瓶应用程序(如下所示:http://flask.pocoo.org/docs/0.12/patterns/packages/)

I have a large flask application built inside a package called "MyApp" (exactly as shown here: http://flask.pocoo.org/docs/0.12/patterns/packages/)

根据 Flask 文档,调试模式应启用以下功能:

According to the Flask documentation, the debug mode should enables the following features:

  1. 它激活调试器
  2. 它会激活自动重新加载器
  3. 它在 Flask 应用程序上启用调试模式.

一开始,我使用以下命令运行了我的烧瓶应用程序,一切正常:

At the beginning I've run my flask application with the following command and everything were worked fine:

export FLASK_APP=MyApp
export FLASK_DEBUG=1 
flask run

然后我阅读了设置配置系统的正确方法(包括调试模式).所以我创建了以下 config.py 文件:

Then I read about the correct way to setup a configuration system (including the debug mode). So I created the following config.py file:

class Config(object):
    DEBUG = False
    ...

class ProductionConfig(Config):
    ...

class DevelopmentConfig(Config):
    DEVELOPMENT = True
    DEBUG = True
    ...

CONFIGS = {
    "development": DevelopmentConfig,
    "production": ProductionConfig,
    "default": DevelopmentConfig
}

在我的应用程序 __init__.py 文件中,我写道:

And in my application __init__.py file, I wrote:

app = Flask(__name__)
config_name = os.getenv('FLASK_CONFIGURATION', 'default')
app.config.from_object(CONFIGS[config_name])

现在,为了运行应用程序,我输入了一个新命令:

Now, to run the application I enter a new command:

export FLASK_APP=MyApp
export FLASK_CONFIGURATION=development 
flask run

不幸的是,这次调试模式根本没有激活..

Unfortunately, this time the debug mode did not activated at all..

未激活调试器或自动重新加载器.唯一改变的是 app.debug 现在等于 True.

No debugger or automatic reloader has been activated. The only thing that has been changed was that app.debug is now equals to True.

我不明白.. 看起来 DEBUG = TRUE 工作不正常.

I don't get it.. It looks like the DEBUG = TRUE is not working correctly.

你知道为什么会这样吗?

Do you have any idea why does it happen?

推荐答案

使用调试器运行不同于设置 DEBUG 配置.你必须两者都做.在开发模式下运行服务器会自动设置配置.通常,您应该依赖它而不是直接设置配置.

Running with the debugger is different than setting the DEBUG config. You have to do both. Running the server in development mode sets the config automatically. Typically, you should rely on that rather than setting the config directly.

您读到的正确配置方式"是 a) 只是另一种方式,而不是正确"方式,并且 b) 仅设置配置,而不是 FLASK_ENVFLASK_DEBUG 环境变量,用于控制服务器的调试模式.

The "correct way to configure" you read about is a) just another way, not the "correct" way, and b) only sets the config, not the FLASK_ENV or FLASK_DEBUG environment variables, which is what controls the debug mode for the server.

设置环境变量 FLASK_ENV=development 告诉 flask run 用调试器和重新加载器包装应用程序.(app.run(debug=True) 做同样的事情,但不能设置 FLASK_ENV,它只有一个环境变量.flask run命令现在是首选).app.debug 切换 Flask 应用程序中的一些内部行为,例如将错误传递给启用了开发模式的交互式调试器.

Setting the environment variable FLASK_ENV=development tells flask run to wrap the application with the debugger and reloader. (app.run(debug=True) does the same but can't set FLASK_ENV, which only has an env var. The flask run command is preferred now). app.debug switches some internal behavior in the Flask app, such as passing through errors to the interactive debugger that development mode enabled.

这篇关于Flask 配置文件 - 'DEBUG=True' 什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆