Can't enable debug mode in Flask

前端 未结 1 1841
遇见更好的自我
遇见更好的自我 2021-01-05 17:35

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 an

相关标签:
1条回答
  • 2021-01-05 17:57

    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).

    0 讨论(0)
提交回复
热议问题