Flask配置文件-'DEBUG = True'不执行任何操作 [英] Flask Config File - 'DEBUG=True' Do Nothing

查看:389
本文介绍了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应用程序上启用调试模式。

一开始我已经运行了使用以下命令的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_ENV FLASK_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 告诉烧瓶运行用调试器和重新加载器包装应用程序。 ( app.run(debug = True)可以执行相同的操作,但不能设置 FLASK_ENV ,该环境只有一个env var。现在首选烧瓶运行命令)。 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天全站免登陆