1

I'm trying to enable debug mode in browser as follows:

from werkzeug.debug import DebuggedApplication  

application = Flask(__name__)
app = application
application = DebuggedApplication(app, True)

But it does not enable debug mode in my production server Apache under mod_wsgi.

Did I have something wrong? Also the export method doesn't work either.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

1

The DebuggedApplication() middleware only works if an uncaught exception reaches it. Flask, in production mode, catches all exceptions so these never reach the middleware.

You need to explicitly tell Flask to not catch all exceptions with:

PROPAGATE_EXCEPTIONS = True

in your configuration. From the relevant documenation:

PROPAGATE_EXCEPTIONS

Exceptions are re-raised rather than being handled by the app’s error handlers. If not set, this is implicitly true if TESTING or DEBUG is enabled.

You can set it on the app object before wrapping it in the DebuggedApplication middleware:

app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)

Note that this only tells Flask to not catch all exceptions any more. You may also want to set other configuration options such as PRESERVE_CONTEXT_ON_EXCEPTION, or you could just enable debug mode with app.debug = True.

davidism
  • 121,510
  • 29
  • 395
  • 339
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • This wordked like a charm thanks! What about `app.debug = True` where should i place this and how in order to work? – Νικόλαος Βέργος Oct 02 '18 at 15:34
  • 1
    @ΝικόλαοςΒέργος: in the same location, instead of `app.config['PROPAGATE_EXCEPTIONS'] = True`. Take into account that setting debug mode on means that template reloading is enabled and some other debug settings on exception handling. – Martijn Pieters Oct 02 '18 at 15:45
  • And what exactly are their difference? Also you seem to know very well and i want to ask you if you can help me with this question too: https://stackoverflow.com/questions/52593499/form-a-url-link-with-utf-8-encoding – Νικόλαος Βέργος Oct 02 '18 at 15:51
  • 1
    @ΝικόλαοςΒέργος: `PROPAGATE_EXCEPTIONS` is automatically enabled when `app.debug` is set, but there are more such settings. Setting `app.debug = True` enables all those settings in one go. – Martijn Pieters Oct 02 '18 at 16:11