[英]Can't enable debug mode in Flask
I have a fairly basic Flask app, but for some reason Debug mode wont enable, so whenever I get an error I get a 500 page instead of the nice debug page with the traceback and all that. 我有一个相当基本的Flask应用程序,但由于某种原因调试模式不会启用,所以每当我得到一个错误,我得到一个500页而不是带回溯的好的调试页面和所有这些。 Here's my app/ init .py:
这是我的app / init .py:
from flask import Flask
from config import config
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
from .api import api as api_blueprint
app.register_blueprint(api_blueprint, url_prefix='/api/v1.0')
return app
and here's my config.py: 这是我的config.py:
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config:
SECRET_KEY = '12345'
SQL_DRIVER = 'SQL Server Native Client 11.0'
SQL_SERVER = 'WIN8\MSSQL2K12'
SQL_DATABASE = 'LogMe'
SQL_USER = 'LogMe'
SQL_PASSWORD = 'password'
@staticmethod
def init_app(app):
pass
class DevelopmentConfig(Config):
DEBUG = True
config = {
'development' : DevelopmentConfig
}
I've posted the whole project up on GitHub if there happens to be an issue elsewhere, but I assume it's somewhere in these two files: https://github.com/jcaine04/perf-dash/tree/master/app 我已经在GitHub上发布了整个项目,如果在其他地方发生了问题,但我认为这是在这两个文件中的某个地方: https : //github.com/jcaine04/perf-dash/tree/master/app
The debugger is part of the WSGI runner ; 调试器是WSGI运行器的一部分; the
app.run()
server. app.run()
服务器。 If you use a different WSGI server you need to explicitly add the debugger middleware: 如果使用其他WSGI服务器,则需要显式添加调试器中间件:
def create_app(config_name):
app = Flask(__name__)
# ...
if app.debug:
from werkzeug.debug import DebuggedApplication
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
return app
When you use Flask-Script , the runserver
runs the Flask WSGI development server and will enable the debugger. 使用Flask-Script时 ,
runserver
运行Flask WSGI开发服务器并启用调试器。
Unfortunately, Flask-Script version 2.0.3 introduced a bug ; 不幸的是,Flask-Script版本2.0.3引入了一个bug ; it doesn't set up the new debug flags correctly and always disabled the debugger unless you explicitly pass in the
-d
flag. 除非您明确传入
-d
标志,否则它不会正确设置新的调试标志并始终禁用调试器。 This is regardless of wether you set use_debugger
to true; 这与你将
use_debugger
设置为true无关; this because the default of an argparse
store_true
option is to store False
instead when not picked. 这是因为
argparse
store_true
选项的默认值是在未选中时存储False
。
The work-around is to explicitly use -d
, or to patch flask_script/commands.py
to give all --debug
and --no-debug
options self.use_debugger
as the default: 解决方法是显式使用
-d
, 或 flask_script/commands.py
以将所有--debug
和--no-debug
选项self.use_debugger
作为默认值:
if self.use_debugger:
options += (Option('-d', '--debug',
action='store_true',
dest='use_debugger',
help="(no-op for compatibility)",
default=self.use_debugger),)
options += (Option('-D', '--no-debug',
action='store_false',
dest='use_debugger',
default=self.use_debugger),)
else:
options += (Option('-d', '--debug',
action='store_true',
dest='use_debugger',
default=self.use_debugger),)
options += (Option('-D', '--no-debug',
action='store_false',
dest='use_debugger',
help="(no-op for compatibility)",
default=self.use_debugger),)
where I've added default=self.use_debugger
to the two options that did not yet have it set. 我已将
default=self.use_debugger
添加到尚未设置的两个选项中。
The handling of self.use_reloader
is similarly flawed. self.use_reloader
的处理同样存在缺陷。
Versions 0.6.7 and 1.0 don't suffer from this bug; 版本0.6.7和1.0不会受此错误的影响; a fix has been committed for version 2.0.4 (not as yet released).
已针对版本2.0.4 (尚未发布) 提交修复程序 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.