Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Has anyone had problems with Django settings files not interpreting environment variables correctly?

-----------------------
EDIT: Solved! Thanks /u/dirtybutter and /u/mipadi!
-----------------------


I feel like I've come across a sort of weird problem here. On my local machine, I work in virtualenvs when I am building things. Then I primarily push projects up to Heroku.

On most of my projects that are public-facing I set them up using env variables for sensitive stuff like DEBUG, SECRET_KEY, etc. Well a couple days ago I had an issue where I couldn't get debug to turn off on one of my projects on Heroku even though I had this line in my settings file:

DEBUG = bool(os.environ.get('DJANGO_DEBUG', True))

On the Heroku side, just like I always do, I set an ENV variable with the cli like so:

heroku config:set DJANGO_DEBUG=" " #Empty string evals to false

So for whatever reason that wasn't working to turn off debug on Heroku, and it seemed like Heroku wasn't interpreting it correctly because when I would print the variable using the python shell, with that same code above (i.e. bool(...)) when running bash on the Heroku dyno it would still come back as true no matter what I set it as. Finally I adjusted the settings file in my project so now it says this:

if os.environ.get('DJANGO_DEBUG') == 'True':
dbg = True
else:
dbg = False

DEBUG = dbg #bool(os.environ.get('DJANGO_DEBUG', True))

That fixed the problem on Heroku. HOWEVER, now when I run the local python server on my local machine during development, all of a sudden I can't load my static files?!?! If I set an env variable locally on my machine called DJANGO_DEBUG and set it to 'True' (note it's a STRING not a boolean) the application still has debug off. If I change the settings file back to what it originally was, DEBUG = bool(os.environ.get('DJANGO_DEBUG', True)), then my static files load again and debug acts normally, but then debug won't be interpreted right on Heroku!

So basically, my question is: WTF!?!?!?! I don't know what changed...if it's a python versioning thing, a heroku thing, or a django version thing, or something totally different. I have at least 10 other projects running on Heroku AND locally right now and I have used this syntax for years so I don't know what the hell changed or why my settings file doesn't work right on my local machine when I use the "long form" if statement, but doesn't work right on Heroku when I use the short form.

Any ideas? Thanks!!

/r/django
https://redd.it/6wl4bq