Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django DEBUG=False still runs in debug mode

Tags:

django

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()
like image 590
awwester Avatar asked Jul 27 '16 15:07

awwester


People also ask

How do I change debug to false in Django settings?

To disable debug mode, set DEBUG = False in your Django settings file.

What is debug false in Django?

Open your settings.py file (or settings_local.py ) and set DEBUG = False (just add that line if necessary). Turning off the Django debug mode will: Suppress the verbose Django error messages in favor of a standard 404 or 500 error page. You will now find Django error messages printed in your arches.

What is debug false?

The DEBUG=True , if there is error, page will show details of error. if DEBUG=False , the ALLOWED_HOSTS of settings.py will work, you should take carefully to set it. the media and static will not provide access for DEBUG=False , you have to provide them with the help of webserver, like Nginx or Apache .


2 Answers

The root cause of issue - False translated to 'False' string not bool.

Used next solution (settings.py):

DEBUG = os.getenv('DEBUG', False) == 'True'

i.e.: in this case DEBUG will set to (bool) True, only if the environment variable is explicitly set to True or 'True'.

Good luck configuring! :)

like image 163
Taras Buha Avatar answered Sep 22 '22 06:09

Taras Buha


DEBUG takes Boolean value.

The os.environ.get("DEBUG", False) returns string value.

All the string value while typecasting return True. That's why the DEBUG is True.

 print(bool(os.environ.get("DEBUG", False))) # output: True
 print(bool("False"))  # output: True
 print(bool(""))  # output: False

To solve issue in heroku don't set any value to config var. it will return null string and after typecasting it will return False: enter image description here

like image 30
Shubham Harinkhede Avatar answered Sep 23 '22 06:09

Shubham Harinkhede