无法在Flask中启用调试模式 [英] Can't enable debug mode in Flask

查看:564
本文介绍了无法在Flask中启用调试模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当基本的Flask应用程序,但出于某种原因,调试模式不会启用,所以每当我得到一个错误,我得到一个500页,而不是与traceback漂亮的调试页和所有。这里是我的应用/ init .py:

  from flask import Flask 
from config import config

$ b def create_app(config_name):
app = Flask(__ name__)
app.config.from_object(config [config_name])
config [ config_name] .init_app(app)
$ b $ from .main import main 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')

返回应用程序

这里是我的config.py:

$ p $ import os

basedir = os.path.abspath(os.path.dirname(__ file__))

类配置:
SECRET_KEY ='12345'
SQL_DRIVER ='SQL Server Native Client 11.0'
SQL_SERVER ='WIN8 \MSSQL2K12'
SQL_DATABASE ='LogMe'
SQL_USER ='LogMe'
SQL_PASSWORD ='密码'

@staticmethod
def init_app(app):
pass

$ b $ class DevelopmentConfig(Config):
DEBUG = True

config = {
'development':DevelopmentConfig
}

整个项目在GitHub上,如果碰巧是其他地方的问题,但我认为它在这两个文件中的某个地方:
https://github.com/jcaine04/perf-dash/tree/master/app

解决调试器是WSGI运行器的一部分 app.run()服务器。如果您使用不同的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)

返回应用程序

当您使用<一个href =http://flask-script.readthedocs.org/en/latest/ =nofollow> Flask-Script , runserver 运行Flask WSGI开发服务器,并启用调试器。



不幸的是,Flask-Script 2.0.3版引入了一个bug ;它不会正确设置新的调试标志,除非明确地传递 -d 标志,否则总是禁用调试器。这是不管你设置 use_debugger 为true;这是因为 argparse store_true 选项的默认值是存储 False
$ b

解决方法是明确地使用 -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 '
action ='store_false',
dest ='use_debugger',
default = self.use_debugger),


options + =(Option(' - d','--debug ',
action ='store_true',
dest ='use_debugger',
default = self.use_debugger),
options + =(Option(' - D',' --no-debug',
action ='store_false',$ b $ dest ='use_debugger',
help =(不兼容),
default = self .use_debugger))

其中我添加了 default = self.use_debugger $ b

处理 self.use_reloader 这两个选项还没有设置。 $ c>也有类似的缺陷。

版本0.6.7和1.0不会受到这个bug的影响;一个修复已经提交了2.0.4版本(还没有发布)。

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. Here's my 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:

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

解决方案

The debugger is part of the WSGI runner; the app.run() server. If you use a different WSGI server you need to explicitly add the debugger middleware:

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.

Unfortunately, Flask-Script version 2.0.3 introduced a bug; it doesn't set up the new debug flags correctly and always disabled the debugger unless you explicitly pass in the -d flag. This is regardless of wether you set use_debugger to true; this because the default of an argparse store_true option is to store False instead when not picked.

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:

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.

The handling of self.use_reloader is similarly flawed.

Versions 0.6.7 and 1.0 don't suffer from this bug; a fix has been committed for version 2.0.4 (not as yet released).

这篇关于无法在Flask中启用调试模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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