Skip to main content

Debug Mode

Django projects can run in debug mode. If you open you project's settings.py file, you will find the following lines:

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

By default, projects run in debug mode (= debug turned on). This mainly implies that exceptions thrown by code in views or other parts of the framework will be displayed in the web browser, with the full stack of function calls and information about many project variables.

For example, if you make the following change to your index() view, it will raise the ZeroDivisionError exception:

def index(request: HttpRequest) -> HttpResponse:
x = 7 / 0

When visiting http://127.0.0.1:8000/myapp/ you will see something like this:

Debug Mode Turned On

The information that Django displays when facing an exception is more or less the same as that provided by Python in a normal console application: name of the exception (ZeroDivisionError), description ("division by zero"), file in which it occurs (views.py), line of code (15), etc. Django adds some more information such as HTTP method, cookies, environment variables, etc.

It is extremely important that the application runs in debug mode only during development. As you can see, when an exception occurs, a lot of information, including parts of the code, is displayed to any user who visited the URL that triggered the error.

When debug mode is disabled, which should happen whenever a web project is shipped to production (that is, when it begins to be used by the end user), if an exception occurs in the code the user will simply see the following:

Server Error 500

The developer will be able to access complete error information using the log files. In the next chapter we will see how to disable debug mode and completely configure a project to make it ready to be shipped to production.