I have a Django webapp, and I'd like to check if it's running on the Heroku stack (for conditional enabling of debugging, etc.) Is there any simple way to do this? An environment variable, perhaps?
I know I can probably also do it the other way around - that is, have it detect if it's running on a developer machine, but that just doesn't "sound right".
First set the environment variable
ON_HEROKU
on heroku:Then in
settings.py
DATABASE_URL
environment variableI think you need to enable the database for your app with:
but it is free and likely what you are going to do anyways.
Heroku makes this environment variable available when running its apps, in particular for usage as:
Not foolproof as that variable might be defined locally, but convenient for simple cases.
might also show other possible variables like:
DYNO_RAM
WEB_CONCURRENCY
but I'm not sure if those are documented like
DATABASE_URL
.The most reliable way would be to set an environment variable as above. If that's not possible, there are a few signs you can look for in the filesystem, but they may not be / are not foolproof
Heroku instances all have the path
/app
- the files and scripts that are running will be under this too, so you can check for the presence of the directory and/or that the scripts are being run from under it.There is an empty directory
/etc/heroku
/etc/hosts
may have some heroku related domains added~ $ cat /etc/hosts <snip>.dyno.rt.heroku.com
Any of these can and may change at any moment.
Your milage may vary
Read more about it here: https://devcenter.heroku.com/articles/config-vars
My solution:
These environment variables are persistent – they will remain in place across deploys and app restarts – so unless you need to change values, you only need to set them once.
Then you can test its presence in your app.:
An ENV var seems to the most obvious way of doing this. Either look for an ENV var that you know exists, or set your own:
more at: http://devcenter.heroku.com/articles/config-vars
Short version: check that the time zone is UTC/GMT:
This is more conservative than
if tz=='UTC\n'
: if in doubt, assume that we are in production. Note that we are saving the timezone to an environment variable becausesettings.py
may be executed more than once. In fact, the development server executes it twice, and the second time the system timezone is already 'UTC' (or whatever is insettings.TIMEZONE
).Long version:
making absolutely sure that we never run on Heroku with
DEBUG=True
, and that we never run the development server on Heroku even withDEBUG=False
. Fromsettings.py
:If you really want to run the development server on Heroku, I suggest you add an environment variable specifying the date when you can do that. Then only proceed if this date is today. This way you'll have to change this variable before you begin development work, but if you forget to unset it, next day you will still be protected against accidentally running it in production. Of course, if you want to be super-conservative, you can also specify, say, a 1-hour window when exceptions apply.
Lastly, if you decided to adopt the approach suggested above, while you are at it, also install django-security, add
djangosecurity
toINSTALLED_APPS
, and add to the end of yoursettings.py
: