Search code examples
django

django DEBUG=False still runs in debug mode


I'm having issues setting my DEBUG = False on my deployed server (on heroku).

I don't believe I've seen this before, but setting debug to false is not getting rid of debug mode for my project. This means that when there are 500 errors it shows the error, and 404 errors are showing all my urls.

What's strange is that when i log into the server and run get the setting value from django.conf import settings settings.DEBUG it shows as False, which is what I set it to for the production server. TEMPLATE_DEBUG is also set to False.

I don't know I've ever seen this before where DEBUG = False but it's still acting in debug mode. Any ideas?

EDIT:

Thought I'd put this little note too because it's very common for people to be getting 500 or 400 errors when switching debug to False. I am not getting any errors, my project is just acting like it's in DEBUG mode.

# settings/dev.py
from .base import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

if not DEBUG:
    ALLOWED_HOSTS = ['localhost']

SECRET_KEY = 'localsecret1234123412341234'


# settings/prod.py
from .base import *

import dj_database_url, os

DEBUG = os.environ.get("DEBUG", False)
TEMPLATE_DEBUG = DEBUG

ALLOWED_HOSTS = ['mydomain.com']
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

DATABASES = {}
DATABASES['default'] =  dj_database_url.config()

Solution

  • I'll put this as an actual answer for people that come after.

    There are three main areas that this can happen, where 'this' is basically:

    I've changed something in my settings, but it doesn't seem to be reflected in the operation of my app!

    1. Server hasn't been reset after code changes made.
    2. Local and Dev settings haven't overridden/or are overriding in unexpected ways
    3. A local Environment Variable is overriding the attempt at hardcoding it

    On Heroku, as in this example, Environment Variables are set this way. A more general guide to using them in Linux enviroments is available here.

    Using an example from the question, you can see the environment variables being used in:

    SECRET_KEY = os.environ["DJANGO_SECRET_KEY"] 
    

    This uses the os library to go out to the system, and check for an environment variable called DJANGO_SECRET_KEY.

    A better example is the:

    DEBUG = os.environ.get("DEBUG", False)
    

    This is good because it tries to get it from the environment, and if that fails, uses the second value in the tuple as the default: in this case, False.

    During the course of debugging, you can hunt down your settings using printenv from the linux shell, which will print out all of the available EnvVars. If they're not there, you can set them in the format:

    export DJANGO_SETTINGS_MODULE=mysite.settings

    In this instance, it's probably better to unset the environment variable, rather than typecasting, as per this related answer.