django DEBUG=False still runs in debug mode

前端 未结 9 663
再見小時候
再見小時候 2021-01-12 21:45

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

相关标签:
9条回答
  • 2021-01-12 22:05

    If somebody is using python-decouple for env variables and having trouble with DEBUG you have to use this line to retrieve booleans:

    from decouple import config
    DEBUG = config('DEBUG', default=False, cast=bool)
    
    0 讨论(0)
  • 2021-01-12 22:05

    In case anyone is using .env file or loads environmental variables, I used the below is effectively load the DEBUG env variable in the right type:

    import os
    
    APP_DEGUB_MODE = os.getenv('APP_DEBUG', False)
    DEBUG = eval(APP_DEGUB_MODE) if isinstance(APP_DEGUB_MODE, str) else APP_DEGUB_MODE
    
    
    0 讨论(0)
  • 2021-01-12 22:14

    As a note for others: I had the same problem; I configured my production website on a VM with gunicorn and nginx, following the tutorial for ubuntu 16.04 from the digital ocean documentation.

    I wasn't able to turn off the debug mode, even when restarting the nginx systemd's service: the solution was to restart also the gunicorn service:

    # systemctl restart nginx gunicorn
    
    0 讨论(0)
  • 2021-01-12 22:17

    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.

    0 讨论(0)
  • 2021-01-12 22:17

    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:

    0 讨论(0)
  • 2021-01-12 22:19

    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! :)

    0 讨论(0)
提交回复
热议问题