How to toggle Django's debug mode?

Published on Aug. 22, 2023, 12:20 p.m.

In Django, the debug mode can be enabled or disabled by modifying the DEBUG variable in the settings.py file. When DEBUG is set to True, Django provides some useful features for development, such as automatic project reload on code changes and detailed error pages with a stack trace [3]. However, it is not recommended to run a Django application in DEBUG mode on a production server as it can expose sensitive information and pose a security risk [2].

To turn off the DEBUG mode in Django, simply set the DEBUG variable to False in the settings.py file [2]. When DEBUG is set to False, Django will no longer provide detailed error pages and stack traces, and some tooling like serving static files may not work as expected [2].

It is also worth noting that there are other ways to debug a Django application, such as using the runserver_plus command provided by django-extensions, which starts the development server and gives you Werkzeug’s debugger on exceptions [1]. However, this should only be used locally, as it can give anyone with a browser the rights to execute arbitrary python code in the context of the server [1].

References:

  • [1] Stack Overflow. “How to debug in Django the good way.” Link
  • [2] Stack Overflow. “How to switch off the Django debug screen.” Link
  • [3] CSDN. “Django的DEBUG模式详解.” Link

Tags: