nothingness
nothingness

Reputation: 1031

environment settings in my django app are set to debug false but in production it acts as if its true

I have local and production settings for my django app that import from base like so

from .base import *

try:
    from .local import *
except:
    pass

try:
    from .production import *
except:
    pass

and in my base I have

import os
import dj_database_url
from .my_pass import SECRET, EMAIL_PASSWORD, EMAIL_USER

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))



SECRET_KEY = SECRET

DEBUG = False

and in my local

import os
import dj_database_url
from .my_pass import SECRET

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))


SECRET_KEY = SECRET

DEBUG = True

and in my production

from django.conf import settings

if not settings.DEBUG:
    import os
    import dj_database_url


    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
    PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))




    SECRET_KEY = os.environ['SECRET_KEY']

    DEBUG = False

But when I tested it out in production by doing this

example.com/jnxejnn

it showed me a list of urls as if DEBUG was set to true. Why is that?

Upvotes: 0

Views: 982

Answers (2)

nothingness
nothingness

Reputation: 1031

ultimately this is what worked for me

from .base import *

if os.getenv('_system_name') == 'OSX':
    from .local import *

else:
    from .production import *

I don't understand why tutorials make this so complicated. I ran printenv to look at the variables I was creating and try to understand why they weren't working. I noticed

_system_name=OSX

and thought I could use this because hopefully heroku didn't have the same name for their server

at the same time in one of my chrome tabs(I had multiple tabs open looking for an answer) I looked at a post on how to use environment variables because this

os.environ['DJANGO_SERVER_TYPE'] == 'production'

kept giving me this error

File "/Users/ray/Desktop/myheroku/practice/src/gettingstarted/settings/__init__.py", line 3, in <module>
if os.environ['DJANGO_SERVER_TYPE'] == 'local':
File "/Users/ray/Desktop/myheroku/practice/bin/../lib/python3.5/os.py", line 683, in __getitem__
raise KeyError(key) from None
KeyError: 'DJANGO_SERVER_TYPE'

so I saw someone using this

os.getenv('TAG') 

and figured I could use it like this

os.getenv('_system_name') == 'OSX'

and now my local works as it's supposed to and my production works as it's supposed to. I was currently trying to get this to work

from .base import *
try:
    from .local import *
except:
    pass

try:
    from .production import *
except:
    pass

which I have been seeking advice on and have been trying to make work for the last 3 days. If my solution isn't proper please let me know why it isn't. but as of right now it's working

Upvotes: 0

Joseph
Joseph

Reputation: 13178

Look at the order of the settings file:

  • first it imports from base: DEBUG is False
  • then it imports from local: DEBUG is True
  • then it imports from production: at this point, DEBUG is True, so your if not settings.DEBUG: block is never entered, and DEBUG is not set to False again.

Thus, DEBUG remains True, as it is set in the local settings file.

I'm not sure what the purpose of your if not settings.DEBUG check is, but I think if you eliminate that condition, it will work as you expect.

[Edit] Though the above did answer your question of "Why is that?", it doesn't really help meet your needs, so I'd recommend making a modification to your settings file like so:

from .base import *

if os.environ['DJANGO_SERVER_TYPE'] == 'local':
    try:
        from .local import *
    except:
        pass

if os.environ['DJANGO_SERVER_TYPE'] == 'production':
    try:
        from .production import *
    except:
        pass

Upvotes: 1

Related Questions